LeetCode:Move Zeroes
2016-05-11 20:30
337 查看
Move Zeroes
Total Accepted: 80502 TotalSubmissions: 181411 Difficulty: Easy
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,
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.
Subscribe to see which companies asked this question
Hide Tags
Array Two
Pointers
Hide Similar Problems
(E) Remove Element
c++ code:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int count = 0;
for(int i=0;i<nums.size();i++)
if(0 == nums[i]) count++;
int k=0;
for(int i=0;i<nums.size();i++)
if(0!=nums[i]) nums[k++] = nums[i];
for(int i=nums.size()-count;i<nums.size();i++)
nums[i] = 0;
}
};
相关文章推荐
- 关于HOG特征的一个Python代码
- youi软件测试计划
- 中英文环境转变
- cf76e Points 【降维】
- UFLDL 11 卷积与池化 convolution pooling
- NYOJ 832合并游戏(状态压缩dp)
- 图 邻接链表的拓扑排序
- asp.net AdRotator控件
- mount命令详解
- 模块hashlib
- ubuntu 安装 右键终端 eclpse 远程终端
- Mysql常用函数
- Android程序报错,不能再console捕捉并定位到错误行,怎么办?
- 在源码环境中编译Android studio项目时的mk脚本编写
- LeetCode:Missing Number
- POJ2975(Nim游戏)
- PHP的常量
- 4种方法让SpringMVC接收多个对象
- 两种方式实现checkBox readonly功能
- C++第5次实验-数组分离