Leetcode65: Move Zeroes
2015-09-19 22:11
274 查看
Given an array
to the end of it while maintaining the relative order of the non-zero elements.
For example, given
be
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
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,
numsshould
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution { public: void moveZeroes(vector<int>& nums) { int count = 0; for(int i = 0; i < nums.size(); i++) { if(nums[i] != 0) { nums[count++] = nums[i]; } } while(count < nums.size()) { nums[count] = 0; count++; } } };
相关文章推荐
- ARM学习记录1
- NIO——buffer
- NIO——Channel
- bzoj-2095 Bridges
- Zookeeper linux下使用
- 自己写的轻量级PHP框架trig与laravel5.1,yii2性能对比
- yii rules 验证详解
- MongoDB操作
- Windows Server 2008 R2网站访问PHP响应慢的解决方法
- 黑马程序员-----反射技术
- Mining Massive Datasets课程笔记(一)
- Java常用类及集合框架小实验
- 偏执的我从Linux到Windows的感受
- iOS 多线程及其他补充
- Guide to installing 3rd party JARs
- 旋转数组的最小数字
- 基于3520D的HDMI产品设计
- CSS Sprite的使用
- 为系统菜单添加图标--------暴力反射
- [转]android中drawable资源的解释及例子