283. Move Zeroes
2016-07-09 22:09
204 查看
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.
不是0的元素直接放在数组前面覆盖,最后不足的长度补0即可。
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.
不是0的元素直接放在数组前面覆盖,最后不足的长度补0即可。
public void moveZeroes(int[] nums) { int len=nums.length; if(len<1) return ; int cnt=0; for(int i=0;i<len;i++) if(nums[i]!=0) nums[cnt++]=nums[i]; for(int i=cnt;i<len;i++) nums[cnt++]=0; }
相关文章推荐
- Android动画全面解析
- C#文件上传
- nagios
- 面试题笔记
- CIFAR-10在caffe上进行训练
- 程序员旅游之吐糟途牛——第一天
- lca入门———树上倍增法(博文内含例题)
- lca入门———树上倍增法(博文内含例题)
- 多系统环境下,设置开机默认进入的系统
- 分析一下 Android 异步消息处理机制到底是如何工作的
- c++用liburl进行http通讯
- 生成六位验证码python代码
- SharePreference存储数据
- 通过Jquery异步获取股票实时数据
- 繁简体(GB-Big5)字符串互转的JAVA方式实现
- cocoaPods错误集锦
- c语言中利用rand()time()srand()....产生随机数【4-5】猜数游戏
- 一个小笔记(3):约瑟夫环
- 自适应simpson积分
- 上传自己写的cacti文档,由于都是图片,自己博客保存一份记录