一天一道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. linux系统性能监控--内存利用率

    Linux提供了对物理内存进行合理.高效的访问并可以访问潜在的海量虚存的技术.虚存通常稍多于操作系统实际拥有的内存容量,以便将较少使用的数据卸载到磁盘存储器上,同时又呈现出系统拥有大量物理内存的假象. ...

  2. 20160211.CCPP体系详解(0021天)

    程序片段(01):01.指针数组.c+02.动态数组.c 内容概要:指针数组 ///01.指针数组.c #include <stdio.h> #include <stdlib.h&g ...

  3. 剑指Offer——“你最大的缺点是什么”回答技巧及范例

    剑指Offer--"你最大的缺点是什么"回答技巧及范例   问题分析:认识自己的缺点是一个巨大的优点, 当HR问到你缺点的时候, 你的机会来了, 请快展示你的自知之明吧!你想把优点 ...

  4. Android support library支持包常用控件介绍(一)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现Material Design设计效果,官方给出了Android support design library 支 ...

  5. Android图表库MPAndroidChart(一)——了解他的本质,方能得心应手

    Android图表库MPAndroidChart(一)--了解他的本质,方能得心应手 我们项目中经常会遇到一些统计图,比如折线图,线形图等,在一些运动健康类的App中尤其的常见,这画起来要命,我以前就 ...

  6. RxJava操作符(05-结合操作)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51656736 本文出自:[openXu的博客] 目录: CombineLatest Join ...

  7. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)

    demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...

  8. Android之Animation动画各属性的参数意思(二)

    现在就来讲讲Animation里这四个标签的属性. 一.这四个标签alpha.scale.translate.rotate共有的属性为: android:duration        动画持续时间, ...

  9. 【NPR】卡通渲染

    写在前面 我的博客讲过好几篇卡通渲染了,比如[Unity Shader实战]卡通风格的Shader(一).[Unity Shader实战]卡通风格的Shader(二).[NPR]漫谈轮廓线的渲染.[S ...

  10. 向Github提交代码时遇到的一些问题

    今天分享一下我的一些小经验,那就是向github提交我们的代码.之前一直是直接使用的浏览器完成的代码的下载任务,没有使用过客户端,为了让自己在工作之前熟练使用GitHub,所以就有了下面的这篇博文了. ...