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.

解法一:

常规做法,设置一个新A数组的下标ind。

遍历过程中遇到elem就跳过,否则就赋给新A数组下标对应元素。缺点是有多余的赋值。

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

解法二:最优美的解法,当遍历过程中遇到elem,就用末尾的元素来填补。

这样甚至遍历不到一次。

注意:从末尾换过来的可能仍然是elem,因此i--,需要再次判断。

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

【LeetCode】27. Remove Element (2 solutions)的更多相关文章

  1. 【LeetCode】27 - Remove Element

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

  2. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  3. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  4. 【LeetCode】027. Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  5. 【easy】27. Remove Element

    删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the ...

  6. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  7. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

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

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

  9. LeetCode OJ 27. Remove Element

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

随机推荐

  1. [ IOS ] 视图控制对象ViewController的生命周期

    init-初始化程序 viewDidLoad-加载视图 viewWillAppear-UIViewController对象的视图即将加入窗口时调用: viewDidApper-UIViewContro ...

  2. 洛谷P1772 [ZJOI2006]物流运输 题解

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...

  3. VueJS如何引入css或者less文件的一些坑

    我们在做Vue+webpack的时,难免会引入各种公共css样式文件,那么我们改如何引入呢?引入时会有那些坑呢? 首先,引入公共样式时,我们在“main.js”里使用AMD的方式引入,即 requir ...

  4. Android wifi无线调试App新玩法ADB WIFI

    Wifi 调试App已经不是什么新鲜的事情了,之前也看过不少,不是使用麻烦就是需要root权限,今个我给大家介绍一款好用的android studio 插件--ADB WIFI. 安装 setting ...

  5. tp 生成静态页

    $this->fetch()返回的是html 可以直接写入到HTML文件内生成静态页

  6. Windows下创建文件的权限问题

    在Windows下如果在某个目录下建立一个文件,那么新建立的文件会默认继承该目录的所有权限(父子关系) 如果将一个文件从一个目录移动到到另一个目录下,那么该文件的权限并不会继承自新目录的权限而是还保留 ...

  7. nGrinder3.4 性能测试框架安装

    转载:https://blog.csdn.net/mbugatti/article/details/53782070 nGrinder3.4 (2016.05.24) 支持JDK1.8 github地 ...

  8. 劣质代码评析——《写给大家看的C语言书(第2版)》附录B之21点程序(六)

    . #include <stdio.h> . #include <time.h> . #include <ctype.h> . #include <stdli ...

  9. C#.NET常见问题(FAQ)-无法直接启动带有类库输出类型的项目怎么办

    我把Driver.cs文件去掉了一行注释,发现报错   右击这个解决方案,选择属性,然后再启动项目中改成MySample   更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http:// ...

  10. 在windows下安装redmine及相关问题

    转载注明出处,adousen的博客http://blog.csdn.net/adousen redmine是一个ticket驱动项目管理工具,与trac等工具相比.它最大特色是能够在一个实例中同一时候 ...