算法题丨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 ...
随机推荐
- VMware Workstation 14 Pro永久激活密钥
1. ZC3WK-AFXEK-488JP-A7MQX-XL8YF 2. AC5XK-0ZD4H-088HP-9NQZV-ZG2R4 3. ZY5H0-D3Y8K-M89EZ-AYPEG-MYUA8 4 ...
- 我的C++学习之旅
说在前面:1.学习缘由及学习途径: 在学了Python,c#(自认为未精通)之后,我决定学一下C++. 于是去网上找视频教程,发现都不适合我这种有一定基础的自学者,要么是不完整的高级教程,要么是零基础 ...
- PAT乙级-1063. 计算谱半径(20)
在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而&q ...
- MapReduce浅析
很早之前就用过Hadoop,但对MapReduce中的具体数据流向过程一直不甚明了,用Python Streamming的方式写了几个MapReduce,对这个过程有了一定的认识. 首先我们知道,Ma ...
- loadrunner录制上传文件,但是回放失败
用Loadrunner录制上传文件,脚本回放时发现,文件没有上传成功,检查脚本发现脚本中没有上传文件的路径. 脚本录制时选择的协议,如果说是socket协议,则不需要上传文件的路径,Loadrunne ...
- input依次输入密码
原理: 一个真正的可以输入的input框,opacity: 0,设定位层级:(视图不可见的) 再来6(n)个input,readyonly,用来显示,type为password,设置好样式:(视图可见 ...
- Ubuntu14.04上修改主机名
Ubuntu14.04上修改主机名 author:headsen chen 2017-10-12 15:41:31 个人原创,转载请注明作者,出处,否则依法追击法律责任 查看主机名:hostname ...
- vue.js设置、获取、删除cookie
项目需要前端获取后台返回的cookie,并以此作判断.我是在main.js入口文件下使用的 具体代码: new Vue({ el: '#app', router, template: '<App ...
- FTP站点设置
0x00前言: 应老师今天教的和题目所需 有了今天的博文 0x01准备: windows server 2008 FTP服务 0x02正文: 1.先安装FTP服务 先打开--服务器管理 点击--添加角 ...
- Intel 移位指令的陷阱(转)
今天发现了一个Intel逻辑左移指令shl的一个bug. 逻辑左移的概念是对给定的目的操作数左移COUNT次,每次移位时最高位移入标志位CF中,最低位补零. 其中OPRD1为目的操作数, 可以是通 ...