[抄题]:

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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么就地操作

[一句话思路]:

用一个指针来控制,就能实现就地操作

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 没有理解:0是while最后一起添加的

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

全是0可以最后一起添加

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

单个指针,实现就地

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

27. Remove Element

[代码风格] :

class Solution {
public void moveZeroes(int[] nums) {
//cc
if (nums == null || nums.length == 0) return;
//ini
int insertPos = 0; for (int num : nums) {
//no 0s
if (num != 0) {
nums[insertPos++] = num;
}
}
//0s
while (insertPos < nums.length) {
nums[insertPos++] = 0;
}
//return
}
}

283. Move Zeroes把零放在最后面的更多相关文章

  1. [LeetCode] 283. Move Zeroes 移动零

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

  2. [leetcode]283. Move Zeroes移零

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

  3. 283 Move Zeroes 移动零

    给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序.例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, ...

  4. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  5. 283. Move Zeroes(C++)

    283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...

  6. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  7. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

  8. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  9. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

随机推荐

  1. linux(redhat) mysql 的安装

    教程链接 注意 1.检查版本32/64位的时候 输入 name -a 输出为 Linux localhost.localdomain 2.6.18-53.el5 #1 SMP Wed Oct 10 1 ...

  2. Centos6.4_X64编译安装php-5.4.17、nginx-1.4.2、mysql-5.6.13

    安装参考: CentOS 6.3编译安装Nginx1.2.2+MySQL5.5.25a+PHP5.4.5 http://www.dedecms.com/knowledge/servers/linux- ...

  3. dynamic_caast操作符

    dynamic_caast操作符,将基类的指针或引用安全的转换为派生类的指针或引用. 原理: 将一个基类对象指针或引用抛到继承类指针,dynamic_cast会根据基类指针是否真正指向继承类指针来做相 ...

  4. 【WCF安全】WCF 自定义授权[用户名+密码+x509证书]

    1.x509证书制作(略) 2.直接贴代码 ----------------------------------------------------------------------服务端----- ...

  5. DbEntry 默认 主键ID为long

    DbEntry 默认 主键ID为long,如果自己表中的主键ID为int,可以通过以下方式修改: public class Company :DbObjectModel<Company,int& ...

  6. 戴尔PowerEdge RAID控制卡使用示例(PERC H710P为例)

    Dell PERC使用示例列表(H710p) 特别说明,本文相关RAID的操作,仅供网友在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使用方法.切勿直接在生产服务器上做相关实 ...

  7. numpy之初探排序和集合运算

    排序 排序 numpy与python列表内置的方法类似,也可通过sort方法进行排序. 用法如下: In [1]: import numpy as np In [2]: x = np.random.r ...

  8. ReactJS开发环境搭建与相关工具介绍

    现在Web开发的技术几年前相比可谓变化之大.各种各样的框架,各种各样的工具,让Web开发效率更高,开发出来的效果更好.同时带来的是开发环境的复杂度相比以前是成倍的增加.ReatJS框架是现在比较流行的 ...

  9. 简述FPGA的一些优势

    优势一: 更大的并行度.这个主要是通过并发和流水两种技术实现. A:并发是指重复分配计算资源,使得多个模块之间可以同时独立进行计算.这一点与现在的多核和SIMD技术相似.但相对与SIMD技术,FPGA ...

  10. 1088 Rational Arithmetic

    题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...