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. phpmyadmin导入大sql文件失败解决办法

    摘自:http://www.xunway.com/info/post/499.asp 昨天小编的一个客户在在利用phpmyadmin导入大sql文件的时候,总是提示错误,反应给小编,小编也是第一次遇到 ...

  2. Android Studio删除工程里面无用的代码和资源

    如果你是一个经常开发android应用程序或者做android维护项目的人,我想说你对我谈论的这个话题,一定会感兴趣的. 因为只有做到了这两点,你的项目生成的apk包才会更小,而不是随着你的开发和维护 ...

  3. 常用的Linux终端

    常用的Linux终端 gnome-terminal (Gnome标配) xfce4-terminal (XFCE4标配) lxterminal (LXDE标配) konsole (KDE标配) 前面3 ...

  4. 前端必杀技之Javascript 第1天

    学习了javascript基本语法和使用DOM进行简单操作   1.引用javascript方法: a.在<script></script>标签中加入js代码,如: <s ...

  5. 机器人学 —— 轨迹规划(Introduction)

    轨迹规划属于机器人学中的上层问题,其主要目标是计划机器人从A移动到B并避开所有障碍的路线. 1.轨迹计划的对象 轨迹规划的对象是map,机器人通过SLAM获得地map后,则可在地图中选定任意两点进行轨 ...

  6. 转AOP 介绍

    来自:http://blog.csdn.net/a906998248/article/details/7514969 这篇也不错,详细介绍了CGLIP http://blog.jobbole.com/ ...

  7. Java API ——Collection集合类 & Iterator接口

    对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...

  8. TCP digest

    TCP在网络OSI的七层模型中的第四层——Transport层,IP在第三层——Network层,ARP在第二层——Data Link层,在第二层上的数据,我们叫Frame,在第三层上的数据叫Pack ...

  9. 整理了一份招PHP高级工程师的面试题

    1. 基本知识点 HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码   说明 100   (继续) 请求者应当继续提出请求. 服务器返回此代码 ...

  10. 内存分配方法 kmalloc()、vmalloc()、__get_free_pages()

    Copyright: 该文章版权由潘云登所有.可在非商业目的下任意传播和复制. 对于商业目的下对本文的任何行为需经作者同意. kmalloc #include <linux/slab.h> ...