283. Move Zeroes
2016-03-03 00:24
399 查看
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.
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.
public class Solution { public void moveZeroes(int[] nums) { int cnt = 0, pos = 0; // 将非0数字都尽可能向前排 for(int i = 0; i < nums.length; i++){ if(nums[i] != 0){ nums[pos]= nums[i]; pos++; } } // 将剩余的都置0 for(;pos<nums.length; pos++){ nums[pos] = 0; } } }
相关文章推荐
- 《Node.js开发实战详解》学习笔记
- 进制的意义和算法
- 计算机独立本科考试
- 谈一谈自己对依赖、关联、聚合和组合之间区别的理解
- 【代码笔记】自定义布局实现瀑布流
- 迷宫问题(广搜)
- 折半查找
- JVM学习笔记9—JIT及其相关参数
- 算法9:概率分析
- Hibernate学习篇03——Oracle sequence
- Android布局管理器总结一:LinearLayout
- 存储过程详解
- oracle之续day06—demo
- apue file i/o 学习笔记
- 【C】论‘\r’和'\n'的纯粹性
- 《JavaScript模式》笔记
- hdu4565 矩阵构造+矩阵快速幂
- codevs 1257
- window下连接ubuntu mysql的问题
- Hello World——CSDN Blog