题目描述:

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

我的代码:

  1. public class Solution {
  2. /*
  3. * @param nums: an integer array
  4. * @return:
  5. */
  6. public void moveZeroes(int[] nums) {
  7. // write your code here
  8. //当数组为空时直接返回
  9. if(nums.length == 0) {
  10. return;
  11. }
  12. boolean b = true;
  13. //判断数组是否全为0,若是,则直接返回
  14. for(int i=0; i<nums.length; i++) {
  15. if(nums[i] != 0) {
  16. b = false;
  17. }
  18. }
  19. if(b == true) {
  20. return ;
  21. }
  22.  
  23. int k = nums.length-1;
  24. int i = k;
  25. while(i >= 0) {
  26. //从后往前找元素值为0的数
  27. while(i>=0 && nums[i]!=0) {
  28. i--;
  29. }
  30. if(i >= 0) {
  31. int temp = nums[i];
  32. for(int j=i; j<k; j++) {
  33. nums[j] = nums[j+1];
  34. }
  35. nums[k] = temp;
  36. k--;
  37. }
  38. }
  39. }
  40. }

LintCode之移动零的更多相关文章

  1. LintCode——尾部的零

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

  2. lintcode:移动零

    题目 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数   样例 给出 nums = [0, 1, 0, 3, 1 ...

  3. [LintCode] 尾部的零

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

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

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

  5. lintcode :Trailing Zeros 尾部的零

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

  6. LintCode #2 尾部的零

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

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

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

  8. [LintCode] Move Zeroes 移动零

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

  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. 【ABAP系列】SAP ABAP WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP WS_DELI ...

  2. Machine Learning 文章导读

    Machine Learning Algorithms Linear Regression and Gradient Descent Local Weighted Regression Algorit ...

  3. String类为什么是final

    String 本身一个对象,对象在jvm启动的时候就要实例化和其他类调用就要实例化,第一性能,第二安全,因为string的高频繁的使用,如果被继承,哪里性能将会大大降低,因为不能被继承,换句话来说就比 ...

  4. python基础-5 冒泡排序、递归

    上节总结 一.上节内容补充回顾 1.lambda func = lambda x,y: 9+x 参数: x,y 函数体:9+x ==> return 9+x func: 函数名 def func ...

  5. type动态创建类

    在一些特定场合,需要动态创建类,比如创建表单,就会用到type动态创建类,举个例子: class Person(object): def __init__(self,name,age): self.n ...

  6. [APIO 2010] [LOJ 3144] 奇怪装置 (数学)

    [APIO 2010] [LOJ 3144] 奇怪装置 (数学) 题面 略 分析 考虑t1,t2时刻坐标相同的条件 \[\begin{cases} t_1+\lfloor \frac{t_1}{B} ...

  7. Python 函数知识总汇

    函数在一个程序起到很重要的作用,那么如何学好函数呢,那函数有什么内容的,总结一下函数的知识归类 1,函数定义 def  函数名(): print("...") 2,函数返回值 re ...

  8. latex中\large的作用域问题

    在毕业论文的写作过程中,遇到了一个\large 作用域的问题.假设下面有三种写法: I am cool \large{you are right}, yeah, yeah, yeah I am coo ...

  9. linux命令截取文件最后n行(所有命令)

    linux命令截取文件最后n行(所有命令) tail -n a.txt > b.txt 联想:系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) una ...

  10. CentOS7 设置电源选项,待机、睡眠、挂起

    设置装有 CentOS7 的笔记本合盖后黑屏进入睡眠模式 systemd 能够处理某些电源相关的 ACPI事件,你可以通过从 /etc/systemd/logind.conf 以下选项进行配置: Ha ...