283. Move Zeroes把零放在最后面
[抄题]:
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:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么就地操作
[一句话思路]:
用一个指针来控制,就能实现就地操作
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 没有理解: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把零放在最后面的更多相关文章
- [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 ...
- [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 ...
- 283 Move Zeroes 移动零
给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序.例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
随机推荐
- Python之MySQLdb
MySQLdb是用于Python链接Mysql数据库的接口,它实现了Python数据库API规范V2.0,基于MySql C API上建立的. 1. MySQLdb安装 (1)安装Mysql,参考上篇 ...
- Activiti工作流学习之流程图应用详解
Activiti工作流学习之流程图应用详解 1.目的 了解Activiti工作流是怎样应用流程图的. 2.环境准备2.1.相关软件及版本 jdk版本:Jdk1.7及以上 IDE:eclipse ...
- C# 通过窗口句柄获取程序路径 图标
转自:http://qqhack8.blog.163.com/blog/static/11414798520113363829505/ C# 通过窗口句柄获取程序路径 图标using System;u ...
- 一个kafka异常
卡夫卡经常超时,而且超时时间固定为三分钟,因为时间长度固定,所以逆推一下,发现现有的配置里面有个session的timeout是三分钟,但是其实后来验证发现真是的原因是connect timeout, ...
- Linux C程序操作Mysql 调用PHP采集淘宝商品
还是继续这个项目. 在上一篇Linux下利用Shell使PHP并发采集淘宝产品中,采用shell将对PHP的调用推到后台执行,模拟多线程. 此方法有一致命缺点,只能人工预判每个程序执行时间.如果判断时 ...
- 在html与php中实现上传图片
form.html文件 <body> 点击浏览按钮添加要上传的文件(*请上传大小不能大于2M的静态图片)<br /> <form enctype="multip ...
- 通过ssh连接git操作
项目又一次从svn切换到git,而且要求使用安全协议ssh连接git.不管是个人还是组织,git上只负责导入公钥,而不会帮你生成.这里先说下怎么生成这个ssh的私钥和公钥. 首先你得找到有ssh命令的 ...
- DHCP(三)
选择阶段:即DHCP客户端选择IP地址的阶段.如果有多台DHCP服务器向该客户端发来DHCP Offer报文,客户端只接受第一个收到的DHCP Offer报文,然后以广播方式发送DHCP Reques ...
- nginx安装及编译参数详解
1.centos下Yum安装 Nginx yum list|grep nginx 发现没有可用的结果通过创建下面的文件在系统中添加nginx仓库的yum配置vi /etc/yum.repos.d/ng ...
- WebSocket :Nginx+WebSocket内部路由策略推送服务器的实现(附可生产环境应用代码)
1.项目背景 前几天写了一篇WebSocket推送的博客:WebSocket :用WebSocket实现推送你必须考虑的几个问题 支持的连接数大概几千个,具体数量依赖于tomcat能并发的线程数,但很 ...