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 ...
随机推荐
- ActiveMQ面试题
什么是activemq activeMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. activemq的作用以及原 ...
- Java中小数保留问题
方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 ...
- YARN学习笔记——Overview and Architecture
YARN的简介 什么是YARN MRv1的架构和缺陷 经典MapReduce的局限性 解决可伸缩性问题 YARN的架构 一个可运行任何分布式应用程序的集群 YARN中的应用程序提交 YARN的其他特性 ...
- 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...
- ②SpringBoot之Web综合开发
Spring boot初级教程 :<SpringBoot入门教学篇①>,方便大家快速入门.了解实践Spring boot特性,本文介绍springBoot的web开发 web开发sprin ...
- bzoj 5093 [Lydsy1711月赛]图的价值——第二类斯特林数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5093 不要见到组合数就拆! 枚举每个点的度数,则答案为 \( n*\sum\limits_{ ...
- ubuntu下使用code::blocks编译运行一个简单的gtk+2.0项目
在具体的操作之前,首先需要安装一些必要的软件.ubuntu下默认安装了gcc,不过缺少必要的Header file,可以在命令行中输入下面的指令安装build-essential套件:sudo apt ...
- linux启动自动挂载分区和/etc/fstab简单修复
让后加的分区能够启动时自动挂载,需要把配置写入文件 /etc/fstab vi /etc/fstab UUID=3f5859e0-592f-42cd-b533-570422fb85be / ext ...
- Java中静态变量、静态代码块、非静态代码块以及静态方法的加载顺序
在研究单例设计模式的时候,用到了静态变量和静态方法的内容,出于兴趣,这里简单了解一下这四个模块在类初始化的时候的加载顺序. 经过研究发现,它们的加载顺序为: 1.非静态代码块 2.静态变量或者静态代码 ...
- Linux学习笔记 -- 硬链接与软连接(转)
原文地址: http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html Linux链接概念 Linux链接分两种,一种被称为硬链接(Har ...