题目

给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序

注意事项

1.必须在原数组上操作
2.最小化操作数

 
样例

给出 nums = [0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].

解题

快速排序思想,以0为界划分

public class Solution {
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
public void moveZeroes(int[] nums) {
// Write your code here
int slow = -1;
int fast = 0;
int n = nums.length;
int x = 0;
while(slow < fast && fast < n){
if(nums[fast]!=x){
slow++;
swap(nums,slow,fast);
}
fast++;
}
}
public void swap(int[] A,int i,int j){
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}

稍作更新

public class Solution {
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
public void moveZeroes(int[] nums) {
// Write your code here
int one = 0;
int fast = 0;
int n = nums.length;
int x = 0;
for(int i=0;i<n;i++){
if(nums[i]!=x) { // 不为0 的向前移动
nums[one] = nums[i];
one++;
}
}
for(int i= one;i<n;i++) // 后面的就是0
nums[i] = x;
} }

lintcode:移动零的更多相关文章

  1. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

  2. lintcode :Trailing Zeros 尾部的零

    题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...

  3. LintCode #2 尾部的零

    计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...

  4. LintCode——尾部的零

    尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...

  5. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  6. [LintCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  7. [LintCode] 尾部的零

    class Solution { public: // param n : description of n // return: description of return long long tr ...

  8. LintCode之移动零

    题目描述: 分析:由于要使非零元素保持原数组的顺序,我只能想出在找到一个0时,逐个移动数组元素使得后一个元素覆盖前一个元素,再将这个0移到后头去. 我的代码: public class Solutio ...

  9. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

随机推荐

  1. Object-c 语法 - NSObject常用方法和反射

    NSObject常用方法 - (BOOL)isKindOfClass:(Class)aClass //判断是否为aClass或者aClass的子类的实例,aClass可以通过[类名 class]获取 ...

  2. CentOS 6.3 安装以及配置Apache php mysql

    准备篇: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dp ...

  3. [转]论window和Linux之长短

    论window和Linux之长短 王垠 http://www.kerneltravel.net/jiqiao/whyLinux.htm — 摈弃 Windows 低效率的工作方式,发掘 Linux 身 ...

  4. Android实现入门界面布局

    Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...

  5. Oracle数据文件在open状态被删除的恢复记录

    1.查看当前状态: SQL> select status from v$instance; STATUS------------OPEN SQL> show parameter name; ...

  6. Valid Palindrome

    leetcode:https://oj.leetcode.com/problems/ 今天A了一个Easy类型的,主要是判断一个字符串是否是回文.东平西凑的还是给弄好了,具体可看下面的要求,或者直接去 ...

  7. Daily Scrum2

    今天我们小组开会内容分为以下部分: part 1: 之前的失败教训: part 2: 针对Anti-spam and anti-abuse module模块的任务分工: part 3: 之后小组成员必 ...

  8. Bootstrap入门四:代码

    1.内联代码 code: 通过 <code> 标签包裹内联样式的代码片段.灰色背景.灰色边框和红色字体. For example, <code><section>& ...

  9. C#外挂QQ找茬辅助源码,早期开发

    这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成.女神使用后找茬等级瞬间从眼明手快升级为三只眼...每次看到这个就会想起那段屌丝与女神的回忆.今天特地把代码更新 ...

  10. 利用 js 实现弹出蒙板(model)功能

    关于 js 实现一个简单的蒙板功能(model) 思路: 创建一个蒙板, 设置蒙板的堆叠顺序保证能将其它元素盖住 position: absolute; top: 0; left: 0; displa ...