[抄题]:

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 安全rm

    先将shell脚本放在某个全局路径下,如/usr/local/bin #!/bin/sh # safe rm # Don't remove the file, just move them to a ...

  2. 51nod 2006 飞行员配对

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...

  3. 如何手玩5h uoj215 果冻运输得到 AC

    最近在大力练习提答颓提答,听说果冻运输很好玩就来试试. 然后玩的停不下来 QAQ ... 于是开一篇博客写一下每个点的解法.(一个个手玩出来的..) 首先我们每次都算什么下一步完后会发生什么在大脑中演 ...

  4. Tornado服务端基本的配置文件(Python)

    web_set = { # -------一般设置-------- 'debug': True, 'port': 8836, 'autoreload': True, 'ui_modules': {}, ...

  5. input子系统框架

    废话不多说,直接进入主题.在驱动insmod后,我们应用层对input设备如何操作?以下以全志a64为实例. 在/dev/input/eventX下(X的形成为后续会分析),是内核把接口暴露给应用层, ...

  6. vue router 传参 获取不到query,params

    千万要注意,获取query/params 是this.$route.query 不是this.$router.query!!!

  7. 学习动态性能表(1)--v$sysstat

    由动态性能表学到的 第一篇--v$sysstat  2007.5.23 按照OracleDocument中的描述,v$sysstat存储自数据库实例运行那刻起就开始累计全实例(instance-wid ...

  8. 关于 webpack-dev-server 热更新出现重复的问题

    关于 webpack-dev-server 热更新出现重复的问题 webpack-dev-server 在前端开发时很方便,可以热更新,只需要配置 webpack.config.js 即可. 但是昨天 ...

  9. PHP 操作XML文档

    <<<操作符需PHP5.3以上版本才能支持,下面程序在wamp环境下测试完成. <?php // Set the content type to be XML, so that ...

  10. bzoj 3998 [TJOI2015]弦论——后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3998 相同子串算多个的话,先求好 right ,然后求一个 sm 表示走到这个点之后有几种走 ...