描述

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 = 3

Your 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的更多相关文章

  1. 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 + ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  6. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  7. 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 ...

  8. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  9. leetcode解题报告(6):Remove Duplicates from Sorted List

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

随机推荐

  1. java中单双引号的区别

    单引号: 单引号包括的是单个字符,表示的是char类型.例如: char  a='1' 双引号: 双引号可以包括0个或者多个字符,表示的是String类型. 例如: String s="ab ...

  2. 其实每个行业都有各自的辛苦,好的程序员并不累,他们乐此不疲(见过太多在职位事业、人生方向上随转如流的人,累了疲乏了就去做别的事情了。必须有自己的坚守和立足的点,自我驱动,否则沦为在别人的体制制度中被驱赶一生)good

    作者:陈柯好链接:https://www.zhihu.com/question/39813913/answer/104275537来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. 在oracle表中增加、修改、删除字段,表的重命名,字段顺序调整

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...

  4. Python 的 Mixin 类(转)

    转1:https://www.cnblogs.com/aademeng/articles/7262520.html 转2:https://blog.csdn.net/u010377372/articl ...

  5. css 小图标 & iconfont 字体图标

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! iconfont 字体图标 我们的需求中,很多时候会看到一些小的图形,或者叫图标,比如天猫网站中:     ...

  6. gradient 渐变

    看了大漠 写的关于 Gradient 的文章,我也想写点以便加深记忆. 首先gradient 分为linear-gradient (线性渐变) 和 radial-gradient(径向渐变),渐变是作 ...

  7. [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步

    本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...

  8. 2 vue学习

    1 vue的核心是数据与视图的双向绑定 2 当viewmodel销毁时,所有的事件处理器都会自动删除,无需自己清理 3 v-model的修饰符解释 .lazy :失去焦点或者按回车键时触发同步 .nu ...

  9. Uniswap详解之一(概览)

    一.Uniswap简介 Uniswap是以太坊上的DEX实现,基于"恒定乘积自动做市"模型,与传统的中心化和DEX具有很大的差别. 主要特点: 无订单簿,无做市商 兑换币具有很低的 ...

  10. LINUX档案权限

    一.ls命令显示目录详细信息 列表显示目录内容 : ls drwxr-xr-x.  4 root   root       4096 Jun 26 02:07 acpi -rw-r--r--.  1 ...