LeetCode283:Move Zeros
2016-03-03 01:01
260 查看
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.
主要思路:保持两个指针,保证两个指针之间[zero_bit,nonzero_bit)范围内都是0,然后调换首个0和首个非零
-
void moveZeroes(int* nums, int numsSize) { int zero_bit = 0; int nonzero_bit = 0; while(nonzero_bit<numsSize) { if(nums[nonzero_bit] != 0) { if(nonzero_bit != zero_bit) { nums[zero_bit++] = nums[nonzero_bit]; nums[nonzero_bit] = 0; }else{ ++zero_bit; } } ++nonzero_bit; } }
相关文章推荐
- [DevExpress]ASP.NET動態設定XtraReport報表參數的方式 5715 ASP.NET 檢舉文章 在ASP.NET中要如何動態設定XtraReport報表參數,或
- *LeetCode 90
- 文件上传
- 网易公开课《Linux内核分析》学习心得-mykernel实验的实践和分析
- 错误记录:Resource ID #0x7f040006 type #0x12 is not valid
- 详解 CALayer 和 UIView 的区别和联系
- 深入探讨 Java 类加载器
- Hibernate学习篇05——联合主键(二)
- ***LeetCode 90 Subsets ii
- thinkphp3.2.3开发商城实战(一)
- 面试
- 栈[顺序栈]
- H-Index II
- IOS--AFNetworking POST与GET请求
- MFC双缓冲绘图
- 20160303 - 升级 Windows 10 版本 1511 后,文件夹默认打开方式为 cmd 的修复
- 个人进阶之路——安卓蓝牙模块(4)
- Linux下使用C语言返回年月日
- W6s数据传输格式
- iOS 第三方 需要 引用的库