LeetCode 283 Move Zeroes
2015-12-14 09:42
267 查看
题目描述
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码
[code] public void moveZeroes(int[] nums) { int t = 0; // 把非0元素移到前面 for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) { nums[t++] = nums[i]; } } // 把后面元素值0 for (int i = t; i < nums.length; i++) { nums[i] = 0; } }
相关文章推荐
- Appwidget控件的使用
- rank--求矩阵的秩
- <uses-permission> tag appears after <application> tag 解决警告
- JBPM4.4:再说活动、任务、事件及在网页中高亮正在执行的活动
- 高通骁龙820亚洲首秀:功耗低,跑分高,体验好--ESM
- mysql_select_db 函数 设置当前操作的数据库
- cross--向量或矩阵的叉乘
- 使用go build 进行条件编译
- dot--向量或矩阵的点乘
- android-ImageLoader(转载于收藏)
- CoordinatorLayout高级用法-自定义Behavior
- 多态,接口
- 矩阵基本运算
- minfree阀值设置详解,后台进程管理教程
- jmeter函数之时间函数__time
- 使用qq邮箱的smpt服务发送邮件一定要记得用ssl
- Hibernate:Hibernate3.3和3.6的配置文件的头不一样
- 工厂模式
- asp.net中URL参数传值中文乱码的三种解决办法
- 7 Steps for becoming Deep Learning Expert