题目

给一个数组 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. 如何用nodejs写入mysql 的blob格式的数据

    So, if the array length equals 4, then the size of blob data in mysql DB must be 4 bytes. And it wor ...

  2. php正则表达式获取表格内容

    <?php $contents = '<tr class=""> <td>508982</td> <td>08,07,01,0 ...

  3. 两个有用的shell工具总结

    shell工具之一:sed sed基础 sed编辑器被称作流编辑器,与常见的交互式文本编辑器刚好相反.文本编辑器可以通过键盘来交互式地插入.删除.替换文本中的数据:而流编辑器是基于一组预先的规则来编辑 ...

  4. 动态链接库知识点归纳之一(DLL概念,如何建立,如何使用,如何优化,如何查看)

    简单的总结一些动态链接库的一些知识,方便以后查找. 首先,新建一个动态链接库 (1)      打开编辑器,选择WIN32项目, dll,如下图,项目名字为:test,选择空项目.如下图 (2)    ...

  5. 微软职位内部推荐-Senior Software Engineer-SDP

    微软近期Open的职位: Position: Senior SDE The R&D of Shared Data Platform at Application and Services Gr ...

  6. Android下写一个永远不会被KILL掉的进程/服务

    Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用.当系统觉得当前的资源非常有限的时候,为了保证一些优先级高的程序能运行,就会杀掉一些 ...

  7. 用C++编写一个随机产生多个两位数四则运算式子的简单程序

    一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...

  8. 【Convert Sorted List to Binary Search Tree】cpp

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  9. IOS常用加密DES

    NSString+DES.h // // NSString+DES.h // haochang // // Created by Administrator on 14-4-15. // Copyri ...

  10. 【转】GCC编译使用动态链接库

    相关gcc参数:-l -L -shared -fPIC -static -c -o   原文地址:[脚本之家]http://www.jb51.net/article/34990.htm     根据链 ...