Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论。

题目链接:https://leetcode.com/problems/remove-element/

题目描述:Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length. 
要注意的是“It doesn't matter what you leave beyond the new length.”

思路分析:思路很简单,直接上代码。

Solution 1:暴力移动

 class Solution {
public:
int removeElement(vector<int>& nums, int val){
int len = nums.size();
int ptr = ;
int newLen = ;
for(int i = ;i<len;i++)
{
if(nums[i]!=val)
{
nums[ptr++]=nums[i];
}
}
newLen = ptr;
return newLen;
}
};

Solution 2:使用STL

 class Solution {
public:
int removeElement(vector<int>& nums, int val) {
auto end = remove(nums.begin(),nums.end(),val);//这里用到了自动指针
return distance(nums.begin(),end);
}
};

关于Solution 2中STL的使用:

1,remove算法描述:查找的得到第一个元素的位置,然后从此位置开始遍历容器,将后面的元素依次前移,跳过和value相同值的元素,也就是说,所有和value相同值的元素都会被覆盖,而其他的元素都会依次前移。最后remove返回"指向最后一个'有用'元素的iterator",但是在remove算法过程中,并没有修改原容器的size,以及end()

2,distance()用于求出迭代器之间的距离,即两个参数之间的元素个数。

[LeetCode] Remove Element 分析的更多相关文章

  1. [LeetCode] Remove Element题解

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

  2. [LeetCode] Remove Element 移除元素

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

  3. LeetCode Remove Element

    原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...

  4. [LeetCode] Remove Element (三种解法)

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

  5. LeetCode——Remove Element

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

  6. [Leetcode] remove element 删除元素

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

  7. leetcode Remove Element python

    class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] ...

  8. LeetCode Remove Element删除元素

    class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...

  9. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

随机推荐

  1. 【PSR规范专题(1)】PSR-0+namespace+spl_autoload_register实现框架模型

    了解命名空间 namespace是PHP5.3版本加入的新特性,用来解决在编写类库或应用程序时创建可重用的代码如类或函数时碰到的两类问题: 用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/ ...

  2. 500G JAVA视频网盘分享 (Jeecg社区)

    http://blog.csdn.net/zhangdaiscott/article/details/18220411    csdn 排名400多名 500 G JAVA视频网盘分享(Jeecg社区 ...

  3. JQuery与DOM中的区别

    一.Query与DOM的区别 1.页面加载: DOM:window.onload=function(){}; JQuery:$(function(){ }); 2.获取对象:JQuery中有“#” D ...

  4. HTTP响应消息code解释

    常见HTTP状态(304,200等) 在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Googl ...

  5. Linux之vi/vim命令

    vi命令是linux中必不可少的一个编辑器工具.那么vi与vim又有什么区别呢,可以简单理解为vim是vi的升级版.在编辑一个文本时,vi不会显示颜色,而vim会显示颜色.显示颜色更易于用户进行编辑, ...

  6. Python中的split()函数的使用方法

    函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(lis ...

  7. Ext2.0之Tabpanel AJAX远程加载多标签页面模式开发技巧

    目前开发的方式是采用远程load页面来实现多页面效果,类似于126邮箱多标签页效果.但是比126邮箱的方式更好,因为页面打开后是load到本地的,126似乎还会重新请求.在近期项目该开发方式已经基本成 ...

  8. C#调用java程序

    前言: 最近跟项目组的人合作一个项目,由于之前我用的是java写的一个与android通信的程序,现在另一个同事来编写界面程序,由于C#编写起来比较方便,而我又不想重新写之前java的那段代码,于是需 ...

  9. float label 提示

    很多时候,我们写input 都会添加 placeholder 属性,用于提示用户这里该输入什么,怎么输入,但是当用户一旦输入了字符串,该提示就会消失,相信会有人,输入内容后可能会忘记这里要输入的是什么 ...

  10. 结构体TABLE_share

    struct TABLE_share { static inline TABLE **next_ptr(TABLE *l) { return &l->share_next; } stat ...