算法题丨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.
Note:
1.You must do this in-place without making a copy of the array.
2.Minimize the total number of operations.
示例
Given nums = [0, 1, 0, 3, 12], after calling your function,
nums should be [1, 3, 12, 0, 0].
算法分析
难度:低
分析:给定一个数组,将所有为0的元素都移动到数组的末尾,并保持非0的元素排序保持不变。
思路:首先,思考满足第1个条件很简单,就是遍历数组,判断当前元素是否为0,如果是0,将0跟当前元素互换一下,遍历完数组就可以了。但是这样处理的话,并不能保证非0元素排序不变,所以,我们放弃这种思路。
那怎么保持非0元素的排序呢?我们考虑记录当前非0的个数索引index,遍历的时候,如果是非0元素,将数组[index]记录该元素,非0的个数索引index加1,下一个非0的就会记录在数组[index+1]中,依次类推,这样其实实现了非0元素顺序保存。最终数组[0,index)即为保持排序的非0元素。剩下的就很简单了,将数组[index]之后的元素全部置0就可以了。
代码示例(C#)
public void MoveZeroes(int[] nums)
{
int index = 0;
for (int i = 0; i < nums.Length; ++i)
{
if (nums[i] != 0)
{
//非0元素,记录排序
nums[index++] = nums[i];
}
}
//非0元素之后的元素全置0
for (int i = index; i < nums.Length; ++i)
{
nums[i] = 0;
}
}
复杂度
- 时间复杂度:O (n).
- 空间复杂度:O (1).
附录
算法题丨Move Zeroes的更多相关文章
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- LeetCode算法题-Factorial Trailing Zeroes(Java实现)
这是悦乐书的第183次更新,第185篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第42题(顺位题号是172).给定一个整数n,返回n!中的尾随零数.例如: 输入:3 输 ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 算法题丨Two Sum
描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 算法题丨3Sum
描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 算法题丨3Sum Closest
描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 算法题丨4Sum
描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
随机推荐
- vue-cli工具搭建vue-webpack项目
1.安装node环境 下载地址 https://nodejs.org/en/download/ node -v 安装成功后在命令行查看node版本 npm-v 安装成功后在命令行查看npm版本 ...
- ASP.NET Core 2.0 : 七.一张图看透启动背后的秘密
为什么我们可以在Startup这个 “孤零零的” 类中配置依赖注入和管道? 它是什么时候被实例化并且调用的? 参数中的IServiceCollection services是怎么来的? 处理管道是怎么 ...
- Linux系统命令归纳
常规操作命令: # netstat -atunpl |egrep "mysql|nginx"# vimdiff php.ini*# runlevel# rpm -e httpd - ...
- ER图
E-R图也称实体-联系图(Entity Relationship Diagram), 提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型. 它是描述现实世界概念结构模型的有效方法.是表示 ...
- SIMD---SSE系列及效率对比
SSE(即Streaming SIMD Extension),是对由MMX指令集引进的SIMD模型的扩展.我们知道MMX有两个明显的缺点: 只能操作整数. 不能与浮点数同时运行(MMX使用FPU寄存器 ...
- Java 面试宝典-2017
http://www.cnblogs.com/nelson-hu/p/7190163.html Java面试宝典-2017 Java面试宝典2017版 一. Java基础部分........... ...
- Spring - JPA 一对一, 一对多, 多对多关联
现在有三个类:One Many Much One类 Much类 @Entity public class Much { @Id @GeneratedValue private Integer id; ...
- thinkphp3.2-更改控制器名后找不到相应的表?报1146的错
用tp在做着自己的小系统的时候,明明在刚才还是能好好地查到表的,在Service用了'D'方法连自己数据库的表,只是更改了自己的控制器名,却报错了... 我就纳闷了,虽然我的控制器和Service用的 ...
- html5 geolocation配合百度地图api实现定位
1.了解html5 geolocation HTML5 Geolocation(地理定位)用于定位用户的位置.鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的.=> 使用时 ...
- node.js与比特币(typescript实现)
BTC中的utxo模型 BTC中引入了许多创新的概念与技术,区块链.PoW共识.RSA加密.萌芽阶段的智能合约等名词是经常被圈内人所提及,诚然这些创新的实现使得BTC变成了一种有可靠性和安全性保证的封 ...