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.

题目的要求大概是给定一个数组和一个值,删除数组中所有和该值相同的元素并返回一个新的长度

说一下思路:

可以采用双指针,下标i循环数组,每次都让p_last下标与i一起遍历,当A[i]与给定的value相同的时候,p_last停止,数组长度-1,i继续走将下一个值赋给A[p_last],如果是A[i]与value不同的话,就让p_last+1

具体的代码如下:

 class Solution {
public:
int removeElement(int A[], int n, int elem) {
int length = n;
int p_last = ;
for(int i=;i<n;i++){
A[p_last] = A[i];
if(A[i] == elem)
length--;
else
p_last++;
}
return length;
}
};

或者可以有一个更加直观的理解:

如下面的代码:

 class Solution {
public:
int removeElement(int A[], int n, int elem) {
int length = n;
int p_last = ;
for(int i=;i<n;i++){
if(A[i]==elem){
length--;
}else{
A[p_last] = A[i];
p_last++;
}
}
return length;
}
};

p_last代表当前”新“数组的下标,i循环数组当A[i]!=elem时进行赋值,并将p_last加一”新“数组的下一个元素等待被插入个人认为这种方式解释方式比较直观。

27 Remove Element的更多相关文章

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

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

  2. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  3. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  4. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  5. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

  6. 【LeetCode】27. Remove Element (2 solutions)

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

  7. 27. Remove Element@python

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

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

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

  9. 【LeetCode】27 - Remove Element

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

  10. LeetCode 27. Remove Element (移除元素)

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

随机推荐

  1. android实现透明和半透明效果

    从透明到半透明时一个值的变化过程. #00000000(全透明)——#e0000000(半透明) 如果觉得半透明的效果太暗淡.可以设置成#60000000,#80000000,#a0000000等等

  2. 疯狂Android第二章:Adapter以及部分控件使用

    第二章 重点:1.理解View以及各种布局的优缺点,适用场景. 2.熟练掌握adapter原理与用法. 3.熟悉其它控件的基本使用方法. /////////////////////////////// ...

  3. dojo.hasClass/dojo.addClass/dojo.removeClass/dojo.toggleClass/dojo.repalceClass

    dojo.hasClass(/*DomNode*/node or DomIdstring,/*String*/classString)//如果节点中有特定的类,那么返回ture,否则返回false d ...

  4. CentOS 7 启动VNC失败问题

    开机后发现VNC服务没有启启来,提示我们使用journalctl -xn查看错误信息,提示信息如下: Sep :: localhost.localdomain systemd[]: Unit vncs ...

  5. jquery 字符个数(数字字母长度记为1,中文记为2,超过长度自动截)

    <script type="text/javascript">  //返回val的字节长度  function getByteLen(val) {  var len = ...

  6. SFTP信任公钥配置及JSCH库

    1.SFTP信用公钥配置 1.1 客户端生成密钥对 以DSA举例: ssh-keygen –t dsa 执行该命令后,在home/用户名/.ssh目录下,会生成id_dsa和id_dsa.pub两个文 ...

  7. hdoj分类

    http://blog.csdn.net/lyy289065406/article/details/6642573 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 ...

  8. 九章算法系列(#2 Binary Search)-课堂笔记

    前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一 ...

  9. linux系统监控常用工具

    linux系统监控常用工具 一.系统核心工具包(coreutils) 1./bin/df 报告系统的磁盘空间用量 df -h  显示磁盘分区fdisk -l 2./bin/uname 显示系统信息 u ...

  10. linux之SQL语句简明教程---LIKE

    LIKE 是另一个在 WHERE 子句中会用到的指令.基本上,LIKE 能让我们依据一个套式 (pattern) 来找出我们要的资料.相对来说,在运用 IN 的时候,我们完全地知道我们需要的条件:在运 ...