【LeetCode】283. Move Zeroes
2016-03-08 10:59
429 查看
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.
[思路]
把非零元素提取出来按顺序前置,后面位置全部置为0.
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.
[思路]
把非零元素提取出来按顺序前置,后面位置全部置为0.
class Solution { public: void moveZeroes(vector<int>& nums) { int len = nums.size(); int count = 0; for(int i =0; i < len; i++) { if(nums[i] !=0) { nums[count] = nums[i]; count++; } } for(int j = count; j < len; j++) { nums[j] = 0; } } };
相关文章推荐
- #1064-You hava an error in your SQL syntax
- k近邻法中kd-tree树的创建
- codeforces #345 div.2 B Beautiful Paintings
- Struts内置类型转换器
- kafka实践
- 纯CSS修改浏览器的默认滚动条样式
- CSS 属性 - 伪类和伪元素
- SocketServer model_use
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
- JavaScript笔记
- Ambiguous column name
- ClamAV源码配置安装测试
- Leetcode:102. Binary Tree Level Order Traversal(JAVA)
- MapGIS6.7_学习中遇到的问题(2):去除飞点
- Android中使用Vectors(2)绘制优美的路径动画
- linux中more与less的区别
- php学习笔记之:环境配置(一)
- RHEL4 安装yum
- wordpress发布文章404
- CocoaPods详解之----使用篇