Leetcode #283 Move Zeroes
2015-09-23 01:48
190 查看
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.
Difficulty: Easy
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.
Difficulty: Easy
</pre><pre name="code" class="cpp">class Solution { public: void moveZeroes(vector<int>& nums) { int len = nums.size(); int zeroNum = 0; for(int i = 0;i<len;i++) { if(nums[i] == 0) zeroNum++; else nums[i-zeroNum] = nums[i]; } for(int i = len-1;i>len-1-zeroNum;i--) nums[i] = 0; return; } };
相关文章推荐
- Root模拟器
- boost库学习之regex
- Datastax文档Apache Cassandra v2.1 第二部分 理解Cassandra架构_数据分发和副本
- Android中基站+GPS定位
- 如何判断 Linux 是否运行在虚拟机上
- coding 除了托管外,还能进行团队协作.
- ListView的使用-2(查看系统应用程序)
- coding 除了托管外,还能进行团队协作.
- CentOS中vsftpd的主动和被动方式
- Listview的Adapter文本内容的两步优化
- onMeasure方法的简单介绍
- Leetcode Shortest Word Distance II
- boost库学习之开篇
- 《构建之法》阅读笔记及项目管理软件
- c3p0配置详解<转贴>
- 国产操作系统厂商编造假新闻艰难度日
- Mybatis c3p0 整合
- coding.net解决github上下载速度慢问题
- Word Search
- Android自定义组合控件之下拉选择框