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. JavaBean的应用

    1. 获取JavaBean属性信息 例1.1 在JSP页面中显示JavaBean属性信息. (1)创建名称为Produce的类,该类是封装商品对象的JavaBean,在Produce类中创建商品属性, ...

  2. 数据库实例: STOREBOOK > 表空间

    ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 表空间(默认) 1. 表空间(默认)返回顶部 1.1, 1.2, 2. 表空间列表(默认)返回顶部 2.1, SYSA ...

  3. super-pow

    // https://discuss.leetcode.com/topic/50489/c-clean-and-short-solution class Solution { ; int powMod ...

  4. opencv学习_5 (IplImage的结构)

    IplImage结构体为: typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; /* 版本 (=0)*/ int nChann ...

  5. iOS开发-Quartz2D初识

    Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...

  6. WebChromeClient 简介 API 案例

    代码位置:https://github.com/baiqiantao/WebViewTest.git 设计思想理解 在WebView的设计中,不是什么事都要WebView类干的,有相当多的杂事是分给其 ...

  7. 黑马day12 DbUtils的介绍

    简单介绍: DbUtils为不喜欢hibernate框架的钟爱.它是线程安全的,不存在并发问题. 使用步骤: 1. QueryRunner runner=new QueryRunner(这里写数据源. ...

  8. ListView GridViewColumn.CellTemplate

    <ListView.View> <GridView> <GridViewColumn Header="Meaningful Use Objectives&quo ...

  9. ObservableCollection

    1)可以使绑定控件与基础数据源保持同步2)还可以在您添加.删除.移动.刷新或替换集合中的项目时引发 CollectionChanged 事件3)还可以在您的窗口以外的代码修改基础数据时做出反应4)相互 ...

  10. Java 调用Web service 加入认证头(soapenv:Header)

    前言 有时候调用web service 会出现 Message does not conform to configured policy [ AuthenticationTokenPolicy(S) ...