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.

题意:删除给定的数,然后返回新的长度。

思路:这题的思路和sort colors差不多,是其简化版。大致的思路是:使用两个指针,指针l 指前,指针 r 指后,遍历数组,遇到给定的数,则将指针 l 指向的元素和r指向的元素交换,每交换一次r--一次,然后重新从指针l处重新遍历;若不是给定的数,则直接跳过即可。代码如下:

 class Solution {
public:
int removeElement(int A[], int n, int elem)
{
int count=;
if(n<) return ;
int l=,r=n-;
while(l<=r)
{
if(A[l]==elem)
{
swap(A[l],A[r])
r--;
count++;
}
else
l++;
}
return n-count;
}
};

思路二:从前向后遍历数组,遇到给定数,记下其位置,将其和后面第一个不为给定数交换,即可。代码如下:

 class Solution
{
public:
int removeElement(int A[],int n,int elem)
{
int count=;
for(int i=;i<n;++i)
{
if(A[i]==elem)
count++;
else if(count>) //避免多个连续
A[i-count]=A[i];
}
return n-count;
}
}

[Leetcode] remove element 删除元素的更多相关文章

  1. LeetCode Remove Element删除元素

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

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

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

  3. lintcode:Remove Element 删除元素

    题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响.  样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 ...

  4. [leetcode]27. Remove Element删除元素

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

  5. 在Python的列表中利用remove()方法删除元素的教程

    在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...

  6. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  7. [LeetCode] Remove Element题解

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

  8. [LeetCode] Remove Element 移除元素

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

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

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

随机推荐

  1. Asp.net 自定义CustomerSession 存放到Redis中

    首先,引用 Redis 操作驱动组件:StackExchange.Redis.dll. 继承SessionStateStoreProviderBase 类, 实现方法: using System; u ...

  2. Asp.Net Core使用Nginx实现反向代理

    ---恢复内容开始--- 前两篇文章介绍了使用Docker作为运行环境,用Nginx做反向代理的部署方法,这篇介绍一下使用Nginx配合.Net Core运行时直接在Liunx上裸奔的方法. 一.安装 ...

  3. MySQL数据库操作(DDL)

    一.创建数据库 语法:create database 数据库名称 [库选项]; 库选项:(可选)数据库的属性,一般有字符集与校对集,保存在数据库所属文件夹下的opt文件 charset:字符集,表示该 ...

  4. memcached搭建

    MemCache 安装使用 安装memcached之前首先需要安装libevent, 如果没有安装的请自行去安装. 下载memcache http://www.memcached.org/files/ ...

  5. JavaSE 第二次学习随笔(二)

    循环结构中的多层嵌套跳出 targeta: for(int i = 0; i < 100; i++){ for (int j = 0; j < 100; j++) { if(i + j = ...

  6. webpack入门概念

    一 概念 1 入口(entry) 入口起点(entry point)提示webpack 应该使用那个模块,来作为构建其内部依赖图得开始.进入入口七点后,webpack 会找出那些模块和库是入口起点(直 ...

  7. 北京Uber优步司机奖励政策(3月25日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. java集合浅谈(一)

    一.类库结构图概览 容器对象仅能持有对象引用(对象的指针),而不是Copy对象信息,从网上搜得几张Java中集合类库的结构图,如下所示: 二.解说Collection 2.1 Collection ( ...

  9. 让webapi支持CORS,可以跨域访问

    1.在NuGet里搜索webapi找到下面的扩展,添加进项目里. 2.在Global.asax中添加一行代码 protected void Application_Start() { //添加CORS ...

  10. vue循环绑定v-model

    直接上代码 结构: <repayInput v-if="formData" v-for="(item, index) in formData" :isPw ...