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

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.


题目标签:Array
  这道题目给了我们一个nums array, 让我们把array 里和val 相同的数字都移除,最后return 一个 新的length。题目规定我们要in place。 所以利用two pointers, i 和 res = 0。 让 i 从0 开始遍历,一旦找到和val 不相同的数字,把这个数字和res 位置的数字互换,res++。意思就是,把所有不需要删除的数字,放到前面去。res记录了所有不需要删除的数字。
 
 

Java Solution:

Runtime beats 95.05%

完成日期:03/27/2017

关键词:Array

关键点:Two Pointers

 public class Solution
{
public int removeElement(int[] nums, int val)
{
int res = 0; // count non-val numbers for(int i=0; i<nums.length; i++)
if(nums[i] != val) // once find the non-val number, swap non-val number with res index
nums[res++] = nums[i]; return res;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4606700.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 27. Remove Element (移除元素)的更多相关文章

  1. [LeetCode] 27. Remove Element 移除元素

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

  2. [LeetCode]27. Remove Element移除元素

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

  3. 27. Remove Element - 移除元素-Easy

    Description: Given an array and a value, remove all instances of that value in place and return the ...

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

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

  5. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

  6. [LeetCode] Remove Element 移除元素

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

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

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

  8. 027 Remove Element 移除元素

    给定一个数组和一个值,在这个数组中原地移除指定值和返回移除后新的数组长度.不要为其他数组分配额外空间,你必须使用 O(1) 的额外内存原地修改这个输入数组.元素的顺序可以改变.超过返回的新的数组长度以 ...

  9. 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. Java:参数数量可变的方法

    许多Java新人在看到下面的这段代码的时候,都会问一个问题:dealArray方法里那三个小点点是什么啊? [java] view plaincopy public class TestVarArgu ...

  2. Oracle中如何插入特殊字符:& 和 ' (多种解决方案)-转载

    文章出处:http://blog.sina.com.cn/s/blog_5f39af320101gb3f.html 今天在导入一批数据到Oracle时,碰到了一个问题:Toad提示要给一个自定义变量A ...

  3. eclipse设置背景保护色及常用设置

    1.设置背景颜色 2.代码自动补全 Windows-->Preferences-->Java-->Editor-->Content Asist,在Auto activation ...

  4. mongodb 面试题总结

    mongodb 面试题总结 1 nosql和关系型数据库的区别 2 nosql数据库有哪些 redis mongodb hbase 3 MySQL与mongodb本质之间最基本的差别是什么 差别在多方 ...

  5. 1.Bootstrap-简介

    1.介绍 Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的. 2.HTML 模板 一个使用了 Boots ...

  6. Linux入门之常用命令(12) mount

    查看Linux所有设备 cd  /dev ls -l sd*  //分区 查看Linux磁盘 fdisk -lu 挂载 一般挂载至/mnt /media mount /dev/sda5   /mnt/ ...

  7. ubuntu12.04添加程序启动器到Dash Home

    ubuntu12.04 dash home中每个图标对应/usr/share/applications当中的一个配置文件(文件名后缀为.desktop).所以要在dash home中添加一个自定义程序 ...

  8. Hive基础(3)---Fetch Task(转)

    我们在执行hive代码的时候,一条简单的命令大部分都会转换成为mr代码在后台执行,但是有时候我们仅仅只是想获取一部分数据而已,仅仅是获取数据,还需要转化成为mr去执行吗?那个也太浪费时间和内存啦,所以 ...

  9. hdu1760博弈SG

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. NDK各个版本链接

    ndk_r15c (July 2017) Windows 32-bit : https://dl.google.com/android/repository/android-ndk-r15c-wind ...