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.

Solution 1:

int removeElement(vector<int>& nums, int val)
{
   /*set a new indexto to record new length, if it is val then jump to next element and do nothing,
    else, keep it. But there are some redundant assignment*/
if(nums.empty())return ;
int length=;
for(int i=;i<nums.size();i++)
{
if(nums[i]!=val)
nums[length++]=val;
}
return length;
}

Solution 2:

int removeElement(vector<int>& nums, int val)
{
   /*the most beautiful solution, when it meets val, assignment this position with the last element, it cost less than once tranversal
    Tips: the element from the last may be val, so i-- is necessary*/
if(nums.empty())return ;
int newlength=nums.size();
for(int i=;i<newlength;i++)
{
if(nums[i]==val)
{
nums[i]=nums[newlength-];
i--;
newlength--;
}
}
return newlength;
}

【LeetCode】27 - Remove Element的更多相关文章

  1. 【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 ...

  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. 怎样用delphi关闭并重新启动 explorer.exe进程

    uses Tlhelp32; function KillTask(ExeFileName:string):integer; const PROCESS_TERMINATE = $0001; var C ...

  2. Evaluate Reverse Polish Notation(堆栈)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. Java NIO读书笔记2

    一.选择器(Selector) Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel ...

  4. URAL1355. Bald Spot Revisited

    1355 其实就是求质因子的个数 这样肯定是最多的 注意一下 除到最后不是1的情况 #include <iostream> #include<cstdio> #include& ...

  5. win7下搭建opengles2.0编程环境

    原帖地址:http://sixgod.org/archives/72   1.下载AMD的OpenGL ES2.0的模拟器,地址: http://www.opengles-book.com/ESEmu ...

  6. ACM - ICPC World Finals 2013 F Low Power

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 有n个机器,每个机器有2个芯片,每个 ...

  7. HDU 1517 (类巴什博奕) A Multiplication Game

    如果n在[2, 9]区间,那么Stan胜. 如果n在[10, 18]区间,那么Ollie胜,因为不管第一次Stan乘上多少,第二次Ollie乘上一个9,必然会得到一个不小于18的数. 如果n在[19, ...

  8. 51nod1125 交换机器的最小代价

    跟做过的bzoj一道置换群的题几乎一样,只是数据范围大了点,那么就用map就好了... #include<cstdio> #include<cstring> #include& ...

  9. 51nod1627 瞬间移动

    打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... //打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... #include<cstd ...

  10. HDU 2066 一个人的旅行【Dijkstra 】

    题意:给出s个起点,d个终点,问从这些起点到达终点的最短距离 因为有多个起点,所以把这多个起点的值设为0 哎= =改了好久的说= = 是因为在代码里面的t,不知道为什么调用dijkstra()函数之后 ...