LintCode-[容易] 539. 移动零
来源:互联网 发布:淘宝的支付方式有哪些 编辑:程序博客网 时间:2024/05/16 10:02
描述:
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项:
1.必须在原数组上操作
2.最小化操作数
样例:
给出 nums = [0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].
思路:
遍历一遍向量,把所有的 0 项删除,并记录 0 的个数,最后在向量后面加上这个数量的 0 。
C++实现:
class Solution {public: /** * @param nums an integer array * @return nothing, do this in-place */ void moveZeroes(vector<int>& nums) { // Write your code vector<int>::iterator it; int count = 0; for (it = nums.begin(); it != nums.end(); ) { if ((*it) == 0) { count++; it = nums.erase(it); } else { it++; } } for (int i = 0; i < count; i++) { nums.push_back(0); } }};
阅读全文
0 0
- LintCode-[容易] 539. 移动零
- LintCode - 539.移动零
- 移动零-lintCode
- LintCode:移动零
- LintCode 539 移动零
- Lintcode 移动零
- LintCode之移动零
- LintCode 移动零 python
- Lintcode移动零
- 移动零-LintCode
- lintcode刷题-移动零 python
- [LintCode 539] 移动零(Python)
- Lintcode Python之移动零
- 539.Move Zeroes-移动零(容易题)
- 数据结构实例<六>(移动“零”)容易
- 容易 报数 (lintcode)
- LintCode【容易】A+B
- LintCode【容易】二进制求和
- Navicat连接oracle数据库报错:ORA-12737 Instant Client Light:unsupported server character set ZHS16GBK
- 剑指offer——第一次只出现一次的字符
- redis 安装
- Linux给新建用户ssh权限问题
- 解决linux下WaitForMultipleObjects的一种方法
- LintCode-[容易] 539. 移动零
- ImageLoader
- 球场大佬【NOIP2017提高组A组模拟8.17】
- JQuery插件实现下拉复选框和后台mybatis接受处理数据
- Carbon Data 文件结构
- 码农不识贝叶斯,虽知数据也枉然
- LaunchMode and Flag
- [LintCode 514] 栅栏染色(Python)
- 远程访问SQL server