一天一道LeetCode系列

(一)题目

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中删除指定的元素,用到erase()函数,注意迭代器会失效

erase()会返回被删除元素的下一个元素的迭代器

*/

class Solution {

public:

    int removeElement(vector<int>& nums, int val) {

        for(auto iter = nums.begin();iter!=nums.end();){

            if(*iter == val){

                iter = nums.erase(iter);//iter指向被删除元素的下一个元素

            }

            else

                ++iter;

        }

        return nums.size();

    }

};

【一天一道LeetCode】#27. Remove Element的更多相关文章

  1. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  2. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  3. LeetCode 27. Remove Element (移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  4. [LeetCode] 27. Remove Element ☆

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  5. LeetCode 27 Remove Element

    Problem: Given an array and a value, remove all instances of that value in place and return the new ...

  6. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...

  7. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  8. Leetcode 27——Remove Element

    Given an array and a value, remove all instances of that value in-place and return the new length. D ...

  9. (双指针) leetcode 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  10. Leetcode 27. Remove Element(too easy)

    Given an array and a value, remove all instances of that value in-place and return the new length. D ...

随机推荐

  1. 安卓高级 特效动画ExplosionField和 SmoothTransition

    本教程所有图片为github上的所无法正常访问请科学上网 SmoothTransition 展示效果 github:源码地址 使用方法 你能通过一行代码使用上面所有的动画 @Override prot ...

  2. Git之(三)辅助命令

    熟悉了Git的基本操作之后,再来研究一下Git为我们提供的基础设施服务.正如一个程序员,只会写代码是远远不够的,还得知道怎么吃饭,怎么睡觉,怎么谈恋爱,怎么耍流氓--会了这些才能写出更好的代码,这就是 ...

  3. Kafka系列之-Kafka监控工具KafkaOffsetMonitor配置及使用

    KafkaOffsetMonitor是一个可以用于监控Kafka的Topic及Consumer消费状况的工具,其配置和使用特别的方便.源项目Github地址为:https://github.com/q ...

  4. Android布局概述

    布局 布局定义用户界面的视觉结构,如Activity或应用小部件的 UI.您可以通过两种方式声明布局: 在 XML 中声明 UI 元素.Android 提供了对应于 View 类及其子类的简明 XML ...

  5. Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  6. Android Studio下导出jar包和aar包

    Android Studio下导出jar包和aar包 jar包和aar包的区别 步骤 1. 创建Android工程 创建工程比较简单,不错复述 2. 创建一个Library(Module) 创建了一个 ...

  7. 【Netty源码解析】NioEventLoop

    上一篇博客[Netty源码学习]EventLoopGroup中我们介绍了EventLoopGroup,实际说来EventLoopGroup是EventLoop的一个集合,EventLoop是一个单线程 ...

  8. 1091. Acute Stroke (30)

    题目如下: One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given ...

  9. android程序崩溃后重启

    有时候由于测试不充分或者程序潜在的问题而导致程序异常崩溃,这个是令人无法接受的,在android中怎样捕获程序的异常崩溃,然后进行一些必要的处理或重新启动 应用这个问题困恼了我很久,今天终于解决了该问 ...

  10. EBS密码加密研究

     DECLARE   v_password_1 VARCHAR2(240);   v_password_2 VARCHAR2(240);   v_password_3 VARCHAR2(240); ...