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. appium实例编写(1)---以ContactsTest.apk 操作为例

    详情参照   http://www.cnblogs.com/puresoul/p/4696825.html#3326873   自己练习一遍 前言: appium环境搭建参照另一篇博客:http:// ...

  2. JavaEE学习路线

    针对很多初识Java者,对如何学习Java.如何学好Java很迷茫,最近刚把JavaEE的东西学完,把我的学习的经验分享给大家,一条适合大多数人的学习路线. 第一部分:Java语言入门阶段 第二部分: ...

  3. 框架应用:Spring framework (三) - JDBC支持

    Spring框架是一个一站式的框架,也就是对很多技术和框架做了封装,使其应用更加简便. JDBC的代码过程 /STEP 1. Import required packages import java. ...

  4. dynamics 365 AI 解决方案 —— 微软布局

    核心提示:微软在 Office365.Azure 云.Dynamics365 上进行人工智能技术的部署,野心不小. 微软在2016年9月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计 ...

  5. WPF控件自适应屏幕

    如果说界面设计,那么自适应问题一定无法避免,今天就来分享一下,wpf如何实现自适应,先看一下效果:(文末添加源代码下载)     基本思路就是用 Grid 的网格,进行宽度的自动填充适应,  不过对于 ...

  6. TETeLasr Cutting System 开机回零问题

    TETeLasr Cutting System 开机回零问题    :打开 "轴信息"    :打开 加工参数-->机器参数-->脉冲当量: X轴==4000 Y轴== ...

  7. JSON依赖的选择

    json-lib 源码:https://github.com/aalmiray/Json-lib/ 最新版本:2.4 不再更新 <dependency> <groupId>ne ...

  8. Beauty Contest 凸包+旋转卡壳法

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27507   Accepted: 8493 D ...

  9. margin:0px auto和text-align:center区别

    (1)margin:0px auto :作用于块级元素,对块级元素进行居中 (2)text-align:center:作用于内联元素,必须放在要居中的内联元素所在的块级元素. 例: (1) <d ...

  10. java人员正确使用IDEA 的方式

    博主是Java开发人员,以前一直都用myeclipse来开发的,说实话感觉myeclipse毫无美感可言,后来经过同事介绍,认识了IDEA,一眼就相中了IDEA黑色的主题风格,自此就抛弃了旧爱myec ...