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.

Hint:

  1. Try two pointers.
  2. Did you use the property of "the order of elements can be changed"?
  3. What happens when the elements to remove are rare?

Subscribe to see which companies asked this question

【思路】

由于返回是数组的数字顺序可以改变,我们可以从后往前遍历。如果遇到的值和给定的val相同,则数组长度len-1,指针向前移动。如果遇到的值和val不同,则向前找到第一个和val值相同的数组元素,然后把这个元素和最后的元素交换位置。len = len - 1,尾指针继续向前移动。

举个例子,初始数组[3,2,2,3,4,3,5,5,3] val = 3

1. 尾指针 notval = len - 1 = 8,此时nums[notval] = 3,则指针向前移动,len - 1,数组变为[3,2,2,3,4,3,5,5]

2. notval = 7,nums[7] = 5 != 3,向前找到第一个等于3的数组元素,nums[5] = 3,此时令nums[5] = nums[7],len - 1,数组变为[3,2,2,3,4,5,5]

3. 重复上述过程,直到向前找不到值为val的数组元素,则可以返回[5,2,2,5,4,5]。

代码如下:

 public class Solution {
public int removeElement(int[] nums, int val) {
if(nums==null || nums.length==0) return 0;
int len = nums.length;
int notval, isval;
for(notval = len - 1; notval >=0; notval--){
if(nums[notval] == val) len = len - 1;
else{
for(isval = notval - 1; isval >= 0; isval--)
if(nums[isval] == val) break;
if(isval < 0) return len;
else{
nums[isval] = nums[notval];
len = len - 1;
}
}
}
return len;
}
}

LeetCode OJ 27. Remove Element的更多相关文章

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

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

  2. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

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

  4. 【LeetCode】27 - Remove Element

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

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

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

  6. Leetcode No.27 Remove Element(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...

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

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

  8. 【LeetCode OJ】Remove Element

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

  9. LeetCode OJ:Remove Element(移除元素)

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

随机推荐

  1. @PostConstruct 和 @PreDestory

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  2. js变量,语句

  3. HTML下直接调用Less文件

    虽然有很多编译Less的插件可以使用 , 但是在开发的时候 , 每修改一次less代码就编译一次less文件 , 很明显效率就太低了 , 接下来为大家介绍一个直接在html的link标签中引入.les ...

  4. 【转】关于spring集合对象的补充

    <span style="font-size:18px;">关于spring集合对象的补充 spring2.0中对集合对象有了改进,新增了一个<util>标 ...

  5. 【第五篇】Volley代码修改之图片二级缓存以及相关源码阅读(重写ImageLoader.ImageCache)

    前面http://www.cnblogs.com/androidsuperman/p/8a157b18ede85caa61ca5bc04bba43d0.html 有讲到使用LRU来处理缓存的,但是只是 ...

  6. 查看源码利器之sublime text 3 配置 Ctags 插件

    最近在看源码的时候发现sublime text 3是很给力的一款软件,小巧精致,这里着重讲解一下Ctags协助编译和跟踪函数 一.安装Package Control (如果Preferences &g ...

  7. 一个突发性的误解C# 引用类型

    最近再看IOCP,结果里面的一个赋值过程,造成了误解. test t1 = new test(); test t2 = new test(); test t4 = new test(); t1= t2 ...

  8. 修改VirtualBox虚拟机默认存储路径及虚拟机迁移方法

    修改默认安装路径 在安装完虚拟机以后发现我的虚拟的磁盘文件是放在C盘的,就想着有没有办法修改默认存储路径.后来发现确实可以修改,修改虚拟机方法如下:"管理"--->" ...

  9. hdu_5925_Coconuts(离散化+dfs)

    题目链接:hdu_5925_Coconuts 题意: 给你一张很大的图,和小于200个的障碍点,问这张图中的联通块有多少个 题解: 由于障碍点只有200个,所以肯定有很多的空白部分,我们将这些空白部分 ...

  10. 自定义MVC框架(一)-(没有基于xml的)

    0.创建oracle的sql语句如下 --创建表 create table userinfo(id number primary key,uname varchar2(20),password var ...