leetcode解题报告(16):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.
分析
遍历这个数组,判断当前元素是否为0,如果为0,就把0删除(erase),并且将0放到(push_back)最后。否则,将迭代器指向后一元素。
考虑到erase操作会使迭代器失效,因此让该函数返回一个迭代器,这样,执行删除元素的操作后,迭代器会自动指向被删除元素的后一元素(相当于把迭代器加1)。
对于把0压到数组末尾的操作,也会带来新的问题:数组的末尾元素不断更新,可能会导致迭代器永远到不了末尾。
为此,需要记录初始状态下数组的尾迭代器,然后在循环体里判断迭代器是否已到了这个尾迭代器,如果到了,直接break。
于是就有了下面的代码:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
auto e = nums.end(); //保存初始状态下数组的尾迭代器
for(auto it = nums.begin(); it != nums.end();){
if(it != e){ //如果还没到达末尾
if(*it == 0){ //判断是否为0
it = nums.erase(it); //删除操作会更新当前的迭代器
nums.push_back(0); //push操作会改变末尾元素
}else //如果不为0,就让迭代器指向下一元素
++it;
}else //如果已到达初始状态下的末尾,就退出,避免进入死循环
break;
}
}
};
然而这段代码还是提示超时了。。。
看来还是naive啊,估计还是尾后迭代器失效,造成了一个死循环(不断地在删除0,又不断地把0添加到末尾,迭代器永远到不了最后)
机智如我,换成用int型变量判断是否到达末尾:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int i = 0;
for(auto it = nums.begin(); it != nums.end();){
if(i != nums.size()){
if(*it == 0){
it = nums.erase(it);
nums.push_back(0);
}else
++it;
}else
break;
++i;
}
}
};
果然ac了。。。
心得
用迭代器遍历vector时,对于会更新、破坏迭代器的操作,一定要慎之又慎啊。
leetcode解题报告(16):Move Zeroes的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 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 ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- leetCode解题报告5道题(九)
题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...
随机推荐
- C++动态内存常见面试题解析
malloc/free和new/delete傻傻分不清?动态内存管理的面试题难道你了?来看这篇文章,包你全会. 1.malloc/free和new/delete的区别 (1)mall ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- Comet OJ Contest 4
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- ASP.NET Core 2.1 中的 HttpClientFactory (Part 1) HttpClientFactory介绍
原文:https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore 发表于:2018年1月 ASP.NET ...
- Pytorch 1.0升级到Pytorch 1.1.0
Pytorch 1.0Pytorch 1.0于2018-12-8发布,详见https://github.com/pytorch/pytorch/releases/tag/v1.0.0 主要更新JIT全 ...
- 基于【 springBoot+jsoup】一 || 爬取全国行政区划数据
一.代码演示 如果中途中断,可进行刷选过滤已拉取省份数据 /** * TODO * * @author kevin * @createTime 2019-11-18 19:37 */ @RestCon ...
- vue的data里面的值是数组时,在更改其某一项的时候,怎么触发视图的重新渲染?
1. 设置对象或数组的值:Vue.set(target,key,value) :2.删除对象或数组中元素: Vue.delete ( target,key) ;3. 数组对象直接修改属性,可以触发视图 ...
- 浅谈javascript中变量作用域和内存(2)
1.无块级作用域 javascript没有块级作用域,这会让其他程序员在理解js代码上很痛苦.在其他很多语言,比如C,大括号括起来的代码块都有自己的作用域 举个例子 if(true) { var na ...
- 解决 React Native:The development server returned response error code: 404
解决方法: 打开android/app/build.gradle compile 'com.facebook.react:react-native:+' 修改为: compile ("com ...
- IPTables 和 Netfilter 框架
前言 防火墙是保护服务器的重要工具. Linux中最常用的基本防火墙软件是iptables.iptables通过与Linux内核网络堆栈(networking stack)中的包过滤钩子(packet ...