[抄题]:

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

[代码风格] :

  1. class Solution {
  2. public void moveZeroes(int[] nums) {
  3. //cc
  4. if (nums == null || nums.length == 0) return;
  5. //ini
  6. int insertPos = 0;
  7.  
  8. for (int num : nums) {
  9. //no 0s
  10. if (num != 0) {
  11. nums[insertPos++] = num;
  12. }
  13. }
  14. //0s
  15. while (insertPos < nums.length) {
  16. nums[insertPos++] = 0;
  17. }
  18. //return
  19. }
  20. }

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. Python之MySQLdb

    MySQLdb是用于Python链接Mysql数据库的接口,它实现了Python数据库API规范V2.0,基于MySql C API上建立的. 1. MySQLdb安装 (1)安装Mysql,参考上篇 ...

  2. Activiti工作流学习之流程图应用详解

    Activiti工作流学习之流程图应用详解 1.目的  了解Activiti工作流是怎样应用流程图的. 2.环境准备2.1.相关软件及版本    jdk版本:Jdk1.7及以上 IDE:eclipse ...

  3. C# 通过窗口句柄获取程序路径 图标

    转自:http://qqhack8.blog.163.com/blog/static/11414798520113363829505/ C# 通过窗口句柄获取程序路径 图标using System;u ...

  4. 一个kafka异常

    卡夫卡经常超时,而且超时时间固定为三分钟,因为时间长度固定,所以逆推一下,发现现有的配置里面有个session的timeout是三分钟,但是其实后来验证发现真是的原因是connect timeout, ...

  5. Linux C程序操作Mysql 调用PHP采集淘宝商品

    还是继续这个项目. 在上一篇Linux下利用Shell使PHP并发采集淘宝产品中,采用shell将对PHP的调用推到后台执行,模拟多线程. 此方法有一致命缺点,只能人工预判每个程序执行时间.如果判断时 ...

  6. 在html与php中实现上传图片

    form.html文件 <body> 点击浏览按钮添加要上传的文件(*请上传大小不能大于2M的静态图片)<br /> <form enctype="multip ...

  7. 通过ssh连接git操作

    项目又一次从svn切换到git,而且要求使用安全协议ssh连接git.不管是个人还是组织,git上只负责导入公钥,而不会帮你生成.这里先说下怎么生成这个ssh的私钥和公钥. 首先你得找到有ssh命令的 ...

  8. DHCP(三)

    选择阶段:即DHCP客户端选择IP地址的阶段.如果有多台DHCP服务器向该客户端发来DHCP Offer报文,客户端只接受第一个收到的DHCP Offer报文,然后以广播方式发送DHCP Reques ...

  9. nginx安装及编译参数详解

    1.centos下Yum安装 Nginx yum list|grep nginx 发现没有可用的结果通过创建下面的文件在系统中添加nginx仓库的yum配置vi /etc/yum.repos.d/ng ...

  10. WebSocket :Nginx+WebSocket内部路由策略推送服务器的实现(附可生产环境应用代码)

    1.项目背景 前几天写了一篇WebSocket推送的博客:WebSocket :用WebSocket实现推送你必须考虑的几个问题 支持的连接数大概几千个,具体数量依赖于tomcat能并发的线程数,但很 ...