LeetCode 283 Move Zeros
Problem:
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].
1. You must do this in-place without making a copy of the array.
2. Minimize the total number of operations.
Summary:
在不复制数组的情况下,将整型数组中的所有0移至数组末尾,其他数相对位置不变。
尽可能减少操作数。
Analysis:
1. 常规方法:用两个指针,其中一个指针向后扫,每找到一个非零元素则与前面指针内容互换。
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size(), k = ;
for (int i = ; i < len; i++) {
if (nums[i] != ) {
swap(nums[i], nums[k++]);
}
}
return;
}
};
但由于交换操作过多,导致效率不高。
2. 同样两个指针,其中一个指针向后扫,每找到一个非零元素则按顺序从数组头往后放,另一个指针记录当前放置位置。
第一个指针扫完时,将第二个指针到数组末尾全部填0即可。
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size(), k = ;
for (int i = ; i < len; i++) {
if (nums[i] != ) {
nums[k++] = nums[i];
}
}
while (k < len) {
nums[k++] = ;
}
return;
}
};
LeetCode 283 Move Zeros的更多相关文章
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- 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 ...
- 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 -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- LeetCode 283 Move Zeroes 解题报告
题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...
- [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 ...
- Java [Leetcode 283]Move Zeroes
题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- 11. 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 ...
随机推荐
- Excel 单元格自定义格式技巧总结
第一部分 Excel 中的单元格格式是一个最基本但是又很高级的技能,说它基本是因为我们几乎天天都会用到它,会用它来设置一些简单的格式,比如日期,文本等等:高级是因为利用 Excel 单元格的自定义格式 ...
- github及其他记录
http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3 https://github.com/open-power-workgroup/Hospit ...
- SQL获取日期格式
) + ' ' + CONVERT(VARCHAR, DATEPART(hh, GetDate())) + ':' + ) AS Date ),) ),) ),) as DateTime) as [D ...
- NHibernate配置
因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...
- SQL Server中解决死锁
SQL Server中解决死锁的新方法介绍 数据库操作的死锁是不可避免的,本文并不打算讨论死锁如何产生,重点在于解决死锁,通过SQL Server 2005, 现在似乎有了一种新的解决办法. 将下面的 ...
- BZOJ2466——[中山市选]树
1.题目大意:给你一棵树,树的每个节点都有一个权值,是0或1,最开始都是0,你可以做一种修改操作,就是把一个节点和它相邻的 节点的权值取反,问最少几次修改能把所有节点的权值变得都是1,最多100个节点 ...
- mysql连表更新
1.需求 有2张表,a表和b表,要把b表的name数据复制到a表中,当2表的id字段一样的时候 UPDATE A a, B b SET a.name = b.my_name WHERE a.id = ...
- STM32F103ZET6 用定时器级联方式输出特定数目的PWM(转载)
STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5.TIM8,共6个.这里需要使用定时器的级联功能,ST的RM0008 REV12的P388和P399页上有说明对于特定的定 ...
- 交叉编译inetutils并配置telnet服务
inetutils集成了许多网络客户和服务程序,主要有,finger, ftp, ftpd, rcp, rexec, rlogin, rlogind, rsh, rshd, syslog,syslog ...
- python项目在windows下运行出现编码错误的解法
在启动文件里面加入 import sysreload(sys) sys.setdefaultencoding('GB2312') 这样在windows下调试运行神马的,就不会报错了. 当然发布时,建议 ...