Move Zeroes
题目:
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
解析:
自己写的代码
1 class Solution { 2 public: 3 void moveZeroes(vector<int>& nums) { 4 vector<int>::size_type i; 5 for(i=0;i < nums.size();i++) 6 { 7 vector<int>::iterator it = find(nums.begin(),nums.end()-i,0); 8 if(it == nums.end()-i) 9 break; 10 else 11 { 12 nums.erase(it); 13 nums.push_back(0); 14 } 15 } 16 } 17 };
最然通过了,但是时间长,感觉想法也比较笨,事实上通过一次遍历即可
1 class Solution { 2 public: 3 void moveZeroes(vector<int>& nums) { 4 int n = nums.size(); 5 int left = 0, right = 0; 6 while (right < n){ 7 if (nums[right]){ 8 nums[left] = nums[right]; 9 left++; 10 } 11 right++; 12 } 13 while (left < n) 14 nums[left++] = 0; 15 } 16 };
转载于:https://www.cnblogs.com/raichen/p/4930095.html
Move Zeroes相关推荐
- [LeetCode] Move Zeroes - 整数数组处理问题
目录: 1.Move Zeroes - 数组0移到末尾 [顺序交换] 2. 一.Move Zeroes 题目概述: Given an arraynums, write a function to m ...
- Leetcode PHP题解--D68 283. Move Zeroes
2019独角兽企业重金招聘Python工程师标准>>> D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元 ...
- [东哥的leetcode刷题日记] leetcode 283 : Move Zeroes
leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: ...
- Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- LeetCode之Move Zeroes
1.题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...
- LeetCode 283. Move Zeroes
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- C#LeetCode刷题之#283-移动零(Move Zeroes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3907 访问. 给定一个数组 nums,编写一个函数将所有 0 移 ...
- LeetCode之283. Move Zeroes
---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...
- 【leetcode❤python】Move Zeroes
#-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列, #把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终 ...
最新文章
- SAP PM 入门系列14 – PM模块与其它模块的集成
- MEMS惯性传感器有哪些趋势?
- java生成tiff_在Java中读取和写出TIFF图像
- count 和列不能一起查am_AM-RB 003 会不会让 Aston Martin 变成下一个法拉利?
- 数据科学家数据分析师_使您的分析师和数据科学家在数据处理方面保持一致
- STM32F103单片机JTAG端口重映射
- dataframe数组做元素_数组 array 矩阵 list 数据框 dataframe
- Java并发编程中级篇(二):使用Semaphore信号量进行多个资源并发控制
- 终于有了自己的域名了
- 数据结构之均摊复杂度的使用(课本)
- 19.看板方法---变异性的根源
- python 数据流中的移动平均值_Python实现数据流中的中位数【堆】
- Fiddler下载视频号中的视频最简教程
- 【工具使用】Modscan32软件使用详解
- 简单20行python代码_就这么简单!20行Python代码爬取腾讯视频
- 请教下 atmega8l 下载程序 下载器progisp1.6.7
- Resilio Sync 无法获取追踪器列表 解决方法
- 基于MATLAB的人脸识别提纲
- 安卓移动开发技术--微信界面设计
- ccproject西西进度计划编制软件最新版11.35发布
热门文章
- 【Java数据结构】Map与Set、搜索树、哈希表
- Intellij Idea 下包建包,无论怎么建都在同一级,已解决(附图)
- php的注入点,Php注入点构造代码
- 调用模块里的action_初级测试人员进阶必备Python编码模块,看过的都说好
- 佳能c3020维修模式 白电平调整_真·迷你微单 佳能EOS M200简评
- 如何接受上级指令_与上级沟通的技巧
- python实现whois查询_利用Python实现域名查询和whois查询
- python 近期用到的基础知识汇总(三)
- python处理数据集并制作词云图
- Python机器学习 集成算法实例