描述

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. asp.net core-2.在vs2017中创建asp.net core应用程序

    今天我们用vs2017创建一个asp.net core 的应用程序,打开vs2017 点击:文件—>项目,选择asp.net core web 应用程序 点击确定 红框内就昨天用控制台去创建的应 ...

  2. kubernetes核心组件kube-proxy

    一.  kube-proxy 和 service  kube-proxy是Kubernetes的核心组件,部署在每个Node节点上,它是实现Kubernetes Service的通信与负载均衡机制的重 ...

  3. ubuntu svn 安装

    deepin@deepin:~$ sudo apt-get install rabbitvcs-rabbitvcs-cli rabbitvcs-core rabbitvcs-gedit rabbitv ...

  4. (六)Activiti之实现学生请假流程

    一.实现学生请假流程 1.1 用activiti插件生成bpmn和png文件 1.2 部署流程定义 package com.shyroke.activiti.firstActiviti; import ...

  5. (五)Struts之Action类基础(二)

    上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...

  6. C#如何调用C++(进阶篇)

    上一篇文章最后,提出的一个问题:如果一个c++库中有很多方法,需要一个个声明??这样岂不是要写很多代码??,而且没有智能提示看到提供了那些方法. 如下图: 如果有朋友需要用这种方式调用,而又不知道怎么 ...

  7. elementui禁用树形结构全部复选框

    需求:编辑回显数据后,禁用树形结构复选框,不可选中,无复选框也不可选中 <el-tabs v-model="activeName" @tab-click="hand ...

  8. python matplotlib以日期为x轴作图

    from datetime import datetime, date, timedelta import matplotlib.pyplot as plt import tushare as ts ...

  9. 实现Vue的双向绑定

    一.概述 之前有讲到过vue实现整体的整体流程,讲到过数据的响应式,是通过Object.defineProperity来实现的,当时只是举了一个小小的例子,那么再真正的vue框架里是如何实现数据的双向 ...

  10. Route Loops

    当网络10.4.0.0发生故障时,RouterC检测到故障,并停止其E0接口的路由报文. 然而,路由器A和B还没有收到失败的通知. 路由器A仍然认为可以通过路由器B访问10.4.0.0.路由器A的路由 ...