Lintcode移动零
来源:互联网 发布:淘宝网首页布局介绍 编辑:程序博客网 时间:2024/05/01 17:39
移动零
给一个数组 nums 写一个函数将 0
移动到数组的最后面,非零元素保持原数组的顺序
注意事项
1.必须在原数组上操作
2.最小化操作数
给出 nums = [0, 1, 0, 3, 12]
, 调用函数之后, nums = [1, 3, 12, 0, 0]
.
public class Solution {
/*
* @param nums: an integer array
* @return:
*/
public void moveZeroes(int[] nums) {
// write your code here
int a =0;
int b=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
a++; //计算出0的个数
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]!=0){
nums[b]=nums[i]; //将非零的数从前到后排
b++;
}
}
for(int i=nums.length-1;i>=0;i--){
if(a>0){
nums[i]=0; //从后往前,有多少个零补多少个。
a--;
}
}
}
}
- 移动零-lintCode
- LintCode:移动零
- LintCode - 539.移动零
- LintCode 539 移动零
- Lintcode 移动零
- LintCode之移动零
- LintCode 移动零 python
- Lintcode移动零
- 移动零-LintCode
- lintcode刷题-移动零 python
- LintCode-[容易] 539. 移动零
- [LintCode 539] 移动零(Python)
- Lintcode Python之移动零
- LintCode-矩阵归零
- lintcode-末尾的零
- LintCode 尾部的零
- LintCode: 矩阵归零
- lintcode ---- 尾部的零
- python操作excel表格
- 下拉菜单的实现
- 求解最小生成树——kruskal算法模板
- Java中==号和equals()方法的区别
- 乘法逆元
- Lintcode移动零
- Unity 进度条
- Python中的random模块(总结)
- 微信小程序例子——获取自己所处的位置坐标
- hdu 4472 Count(简单递推)
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- C++ 基础知识四
- Angular 4.x 学习导引
- pc 电脑web浏览器js通过usb控制安卓手机打电话挂电话