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. 判断字符串是否为UTF8编码

    UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码.由Ken Thompson于1992年创建.现在已经标准化为RFC 36 ...

  2. 顺序栈和链式栈(C++实现)

    顺序栈,是一种基于数组的存储表示. 实现类代码如下: template<class T> class SeqStack{ T *element; int top; int maxSize; ...

  3. 一些常见的CFD基本概念(飞机为例)(摘抄)

    分享一些常见的,常用的但不容易掌握的CFD基础概念. 1.理想气体:不考虑流体粘性的影响. 2.不可压缩流体/恒密度:不考虑流体密度的变化.

  4. JS跨域访问问题

    js跨域了. 只能给几个资料参考了:http://blog.csdn.net/lovingprince/article/details/2954675 http://www.kuqin.com/web ...

  5. openstack私有云布署实践【13.2 网络Neutron-compute节点配置(办公网环境)】

    所有compute节点 下载安装组件   # yum install openstack-neutron openstack-neutron-linuxbridge ebtables ipset -y ...

  6. 【定位:PDF文件定位关键字所在坐标和页码】

    iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...

  7. 2016年团体程序设计天梯赛-决赛 L1-5. 是不是太胖了(5)

    据说一个人的标准体重应该是其身高(单位:厘米)减去100.再乘以0.9所得到的公斤数.已知市斤是公斤的两倍.现给定某人身高,请你计算其标准体重应该是多少?(顺便也悄悄给自己算一下吧……) 输入格式: ...

  8. Trouble Shooting

    情况是这样的,我在写一个类似于Online-Judge的系统,用python很容易实现,编译源程序,运行程序,最后比较程序输出与标准输出得出成绩.现在有个问题,万一程序运行时崩溃,比如出现除0异常,a ...

  9. 带密钥的sha1加密

    带密钥的sha1加密: private static string HmacSha1Sign(string jsonStr, string secretKey, string enCoding ) { ...

  10. c/c++常用的几个关键字总结

    一.volatile volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据.如果没有volatile关键字,则编 ...