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 ...
随机推荐
- NOIP模拟题 膜法
题目大意 给定若干组询问求$\sum\limits_{i=l}^r \dbinom{i}{k}$. 最终输出每组询问答案的乘积. 题解 首先把$l,r$分开处理相减,只需要求$\sum\limits_ ...
- timer用作timestamp及其他
niosii中使用时间戳是很有用的,可以查看代码的执行时间是多少,在使用timestamp的过程中遇到一些问题现在做一下记录. 1.硬件部分构建软核没什么,就加一个timer就行了,加完之后自动获得基 ...
- 洛谷 P1227 [JSOI2008]完美的对称
传送门 题目大意:求一些点集的公共对称中心 题解:对称中心是可以确定的,再判断. 代码: #include<iostream> #include<cstdio> #includ ...
- Oracle终极数据恢复,孰弱孰强(DUL vs AUL)
这几天在帮朋友作数据恢复,由于已经到了无可救药的地步,只能使用终极手段进行恢复,直接从文件中读取数据进行恢复. 在恢复过程中反复对比了DUL和dcba的AUL,感觉到了两者的不同. DUL在处理文件损 ...
- bzoj 3501 PA2008 Cliquers Strike Back——贝尔数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3501 用贝尔三角形 p^2 地预处理 p 以内的贝尔数.可以模(mod-1)(它是每个分解下 ...
- bzoj 3456 城市规划——分治FFT / 多项式求逆 / 多项式求ln
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 分治FFT: 设 dp[ i ] 表示 i 个点时连通的方案数. 考虑算补集:连通的方 ...
- dirname 和 basename
dirname 和 basename 命令 [root@localhost /]# cat /tmp/a.sh dirname $0 #获取脚本所在的路径 basename $0 ...
- Google Cloud VM上在线扩硬盘
Google Cloud VM是可以在线扩展Disk的大小的. 一.创建VM和磁盘 比如我有一台VM,附加了一块Disk,大小是120GB.如下图: 在VM中进行格式化: mkfs.ext4 -F / ...
- curl -I 说明
curl -I 查看header头信息
- PHP 判断手机号归属地 和 运营商的免费接口
在项目开发的时候,需要去查询又一批手机号或者固话的具体信息(归属地 运营商) 就需要写一个脚本,来批量请求接口来得到我们想要的数据 学习源头:https://blog.csdn.net/shaerdo ...