LeetCode283:Move Zeros
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.
主要思路:保持两个指针,保证两个指针之间[zero_bit,nonzero_bit)范围内都是0,然后调换首个0和首个非零
void moveZeroes(int* nums, int numsSize)
{
int zero_bit = ;
int nonzero_bit = ;
while(nonzero_bit<numsSize)
{
if(nums[nonzero_bit] != )
{
if(nonzero_bit != zero_bit)
{
nums[zero_bit++] = nums[nonzero_bit];
nums[nonzero_bit] = ;
}else{
++zero_bit;
}
}
++nonzero_bit;
}
}
LeetCode283:Move Zeros的更多相关文章
- [LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- Leetcode Move Zeros
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode 283 Move Zeros
Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...
- Leetcode-283 Move Zeroes
#283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- leetcode-easy-array-283 move zeros
mycode 77.24% class Solution(object): def moveZeroes(self, nums): """ :type nums: Li ...
- [刷题] 283 Move Zeros
要求 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 实现 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostr ...
- LeetCode 283
Move Zeros Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- LeetCode Animation 题目图解汇总(持续更新中...)
我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub ...
随机推荐
- C# 对List成员排序的简单方法
网上看到的方法,实在太方便了,转过来保存,原链接: http://blog.csdn.net/wanzhuan2010/article/details/6205884 using System; us ...
- android 单词
inflate: 胀, 膨, 通货膨胀, 膨胀系数
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...
- C#序列化XML至对象
内容来源: http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html#_label0 自己做的Demo下载地址:Demo
- 制作自己的Cydia发布源
http://patrickmuff.ch/blog/2013/02/15/create-your-own-cydia-repository-on-ubuntu/ http://www.saurik. ...
- MySQL连接问题浅析
MySQL的客户端,无论是PHP或者Java,都会发起多个连接来提高系统的吞吐量.在云里面的服务器,因为一些设计和实现上的不同,有一些问题被放大了,同时也带了一些新的问题. 连接的超时时间 在Azur ...
- JVM——垃圾收集器
概念补充 并行(Parallel):指多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态. 并发(Concurrent):指用户线程与垃圾收集线程同时执行(但不一定是并行的,可能会交替执行),用 ...
- Android View绘制13问13答
1.View的绘制流程分几步,从哪开始?哪个过程结束以后能看到view? 答:从ViewRoot的performTraversals开始,经过measure,layout,draw 三个流程.draw ...
- aspose.word 在书签处插入符号
doc.Range.Bookmarks["CBJYQQDFS110"].Text = ""; Aspose.Words.DocumentBuilder buil ...
- Oracle数据库“Specified cast is农田valid”
这种错误是笔者在执行一条计算符合条件的行有多少个,用OracleDataReader读取计算出的行数时发生. 查询语句为: Select Count(1) FROM HP_TS Where TS_ID ...