leetcode解题报告(8):Remove Element
描述
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.
分析
遍历该vector,判断当前元素是否和给定val相等,如果相等,就将该值删除,否则就遍历下一个元素,需要注意的一点是调用erase函数会使迭代器失效。如:
vector<int>vi = {1,2,3,4,5,6};
for(auto i = vi.begin(); i != vi.end(); ++i){
if(*i == 3)
vi.erase(i); //error:iterator i is invalid
}
调用vi.erase(i),会使迭代器i失效,在这种情况下,对迭代器做任何操作都会出错。正确的方法是给定一个返回值,该返回值指向被删除的元素的下一元素:
vector<int>vi = {1,2,3,4,5,6};
for(auto i = vi.begin(); i != vi.end();){
if(*i == 3)
i = vi.erase(i); //error:iterator i is invalid
else
++i;
}
代码如下:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
auto iter = nums.begin();
while(iter != nums.end()){
if(*iter == val)iter = nums.erase(iter);
else ++iter;
}
return nums.size();
}
};
也可以不用erase函数,另一种解法如下:
https://discuss.leetcode.com/topic/20654/a-simple-c-solution
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int index = 0;
for(int i = 0; i != nums.size(); ++i){
if(nums[i] != val){
nums[index] = nums[i];
++index;
}
}
return index;
}
};
leetcode解题报告(8):Remove Element的更多相关文章
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- Leetcode 题目整理-7 Remove Element & Implement strStr()
27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- leetcode解题报告(6):Remove Duplicates from Sorted List
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
随机推荐
- java——值传递和引用传递
值传递 在方法被调用时,实参通过形参把它的内容副本传入方法内部,此时形参接收到的内容是实参值的一个拷贝,因此在方法内对形参的任何操作,都仅仅是对这个副本的操作,不影响原始值的内容. 先来看个例子: p ...
- prometheus+grafana监控redis
prometheus+grafana监控redis redis安装配置 https://www.cnblogs.com/autohome7390/p/6433956.html redis_export ...
- office2019激活码 最新各个版本激活码
office2019专业版激活码 激活秘钥 一.office2019激活6月更新 [Key]:F4QWT-NMMKH-XPTV9-W9HFB-B4JCQ [剩余次数:900000+] office20 ...
- [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- ajax:用于创建快速动态网页的技术
ajax是一种用于创建快速动态网页的技术. 异步的javascript和XML(JSON),主要是完成一个局部刷新. 异步:你传输吧,我先干我自个儿的事,你传好了告诉我一声 同步:你传输,我停下活儿看 ...
- JavaScript--常用对象的属性及方法(2)
Array对象(数组) 数组最常用属性:length 获取数组的元素个数 方法: toString() 将数组转换为字符串 var arr = ["武汉市","成都市&q ...
- Python 遍历文件夹清理磁盘案例
import os suffix_name_list = [".pdb", ".ilk"] def find_file(path): # 遍历文件夹 for i ...
- Spark学习笔记2——RDD(上)
目录 Spark学习笔记2--RDD(上) RDD是什么? 例子 创建 RDD 并行化方式 读取外部数据集方式 RDD 操作 转化操作 行动操作 惰性求值 Spark学习笔记2--RDD(上) 笔记摘 ...
- Free lunch is over
译文:http://www.mamicode.com/info-detail-1324737.html 原文:http://www.gotw.ca/publications/concurrency-d ...
- SNMP OID列表
zabbix的snmp监控还没开始讲,不过先给大家列一些snmp常用的一些OID,比如cpu.内存.硬盘什么的.先了解这些,在使用snmp监控服务器. 系统参数(1.3.6.1.2.1.1) OID ...