Move Zeros
来源:互联网 发布:金十数据手机安卓版 编辑:程序博客网 时间:2024/04/27 15:24
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.
解法:使用双指针,将非零的数字全都往前存储,将后面的位置置为0即可。
void moveZeroes(vector<int>& nums) { int pos = 0; for (int i = 0; i < nums.size(); i++){ if (nums[i] != 0){ nums[pos++] = nums[i]; } } for (; pos < nums.size(); pos++){ nums[pos] = 0; }}
参考:http://segmentfault.com/a/1190000003768716
0 0
- move zeros
- Move Zeros
- Move Zeros
- 【leetcode】283. Move Zeros
- Leetcode Move Zeros
- (java)leetcode Move Zeros
- move zeros leetcode
- 【leetcode】:move zeros
- leetcode Move Zeros
- leetcode之move zeros
- Leetcode283 Move Zeros
- 283.move to zeros
- leetcode之 Move Zeros
- leetcode:Move Zeros
- 283.[LeetCode]Move Zeros
- LeetCode: Move Zeros
- leetCode:283.Move Zeros
- 283. Move Zeros
- SimpleAdapter适配器
- python字符串连接,截取,替换操作
- awk打印第一个字母
- 108 ubuntu下安装composer
- JAVA eclipse 编译类文件路径
- Move Zeros
- hdoj 2669 Romantic 【扩展欧几里得 求解最小非负解】
- C -金沙粒 1
- 查到的别人稀疏表示的研究
- Java——网络编程(一)
- 移动端布局 Css3 弹性盒子模型封装 display:box;
- IOS 控制系统音量
- CGI与WSGI编程简析
- DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command f