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. HEOI2016游记

    DAY -1: 省选前集训因为某些事情感觉心情和状态异常的糟糕 坐在窗户上吹了半个小时的冷风之后觉得回家休息一段时间 (反正我家在保定,大不了我省选的时候直接去河北大学就好辣) 在家里颓了三天吧,想清 ...

  2. tomcat源码导入eclipse

    1. 获取源代码 方式一:从官网http://tomcat.apache.org/download-70.cgi 直接下载,官网提供了Binary 和 Source Code两种下载方式,要研究tom ...

  3. 2014--9=17 软工二班 MyEclipse blue==4

    package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...

  4. PowerDesigner连接Oracle数据库建表序列号实现自动增长

    原文:PowerDesigner连接Oracle数据库建表序列号实现自动增长 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties — ...

  5. PHP获取服务器的mac地址类

    PHP获取服务器的mac地址类,不是客户端的. <?php class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 va ...

  6. 转:在MyEclipse+Hibernate

    源地址http://blog.renren.com/share/270733118/5528463779 1.实验环境准备 MyEclipse8.6 Tomcat6.0.20 MySQL5.1 ...

  7. jpa懒加载异常

    1.项目背景概述 事情是这样子的,使用了spring data jpa的项目jeesite jeesite的实体中使用了懒加载模式. 并且一个实体类中还不止一个属性设置了懒加载模式. 项目本身已经存在 ...

  8. iOS方法封装

    (void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...

  9. 面试题_82_to_87_Date、Time 及 Calendar 的面试题

    82)在多线程环境下,SimpleDateFormat 是线程安全的吗?(答案)不是,非常不幸,DateFormat 的所有实现,包括 SimpleDateFormat 都不是线程安全的,因此你不应该 ...

  10. Git show-branch显示提交信息

    git中查看日志,我们用的比较多的就是 git log 以及带一些参数,如: 以一行显示提交日志: $ git log --pretty=oneline 显示最后的几次提交日志: $ git log ...