Leetcode练习题Remove Element

Question:

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

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

Example 1:

Given nums = [3,2,2,3], val = 3,

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

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

Solution:

class Solution {
public int removeElement(int[] nums, int val) { int newIndice = 0; for(int i=0;i<nums.length;i++)
{
if(nums[i]!=val)
{
nums[newIndice++] = nums[i];
}
} return newIndice; }
}

在本题中,主要考点是利用两个指针来实现数组内的内容判断和复制。比如,在本题中我利用newIndice来作为一个比原始指针慢的指针来指向过滤后内容的值,并自增,就实现了目标,得到了新的数组。

另外有趣的是,我将本题看成计算数组中有多少个不重复数组的值;另外延伸一下,再计算每个不重复值的个数;

接下来我们一次考虑:

  • 计算数组中有多少个不重复的值:

思路是使用Hashset来计算,因为HashSet中,每个值仅允许出现一次,所以当我们将值都add进去的时候。

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator; public class hashSet {
public static void main(String[] args)
{
int[] arrays = {1,4,5,2,6,1,10,3};
hashset(arrays);
} public static void hashset(int[] arrays)
{
HashSet hashset = new HashSet();
for(Integer a: arrays)
{
hashset.add(a);
} Iterator iterator = hashset.iterator(); while (iterator.hasNext())
{
System.out.println(iterator.next());
} }
}

Leetcode练习题Remove Element的更多相关文章

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

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

  2. LeetCode 027 Remove Element

    题目要求:Remove Element Given an array and a value, remove all instances of that value in place and retu ...

  3. LeetCode 27. Remove Element (移除元素)

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

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

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

  5. LeetCode 27 Remove Element

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

  6. 【leetcode】Remove Element

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

  7. 【leetcode】Remove Element (easy)

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

  8. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...

  9. Java for LeetCode 027 Remove Element

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

随机推荐

  1. git 创建分支 提交到远程分支

    git 创建分支 并 提交到远程分支 git branch 0.可以通过git branch -r 命令查看远端库的分支情况 1,从已有的分支创建新的分支(如从master分支),创建一个dev分支 ...

  2. Spring框架的相关介绍

    Spring是一个开源轻量级的框架,它的核心是控制反转(IOC)和面向切面编程(AOP). 作为业务层框架的spring能够很好地整合表现层跟持久层. IOC:将类的创建和依赖关系写到配置文件里,可以 ...

  3. WPF 使用动画设置特殊值的方法

    例如设置Visibility属性时: 第一种方式: <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIEleme ...

  4. commonDispatchException 函数的逆向

    看书中给出的内容: 1:在栈中构建 EXCEPTION_RECORD 结构体 2. 根据函数传递参数逆推得到 "判断先前模式"的反汇编代码

  5. windows server 2008 r2 安装 vs2017 无法进入安装界面问题解决方法

    在 windows server 2008 r2 版本操作系统上安装 vs2017 经常出现下载进度条结束后没有任何反应问题,一般是因为安装程序兼容性造成的,解决方案如下: 将 C:\Program ...

  6. .net 通过反射实现两个相同结构实体类的转换

    public static T2 CopyToModel<T1, T2>(T1 source) { T2 model = default(T2); PropertyInfo[] pi = ...

  7. python基础(4):用户交互、if判断、while循环、break和continue

    1. 用户交互 使⽤input()函数,可以让我们和计算机互动起来 语法: 内容 = input(提⽰信息) 这⾥可以直接获取到⽤户输入的内容 content = input("你吃了么?& ...

  8. css3自适应布局单位vw,vh详解

    视口单位(Viewport units) 什么是视口? 在桌面端,视口指的是在桌面端,指的是浏览器的可视区域:而在移动端,它涉及3个视口:Layout Viewport(布局视口),Visual Vi ...

  9. jquery-uploadfile的使用(多文件异步上传)

    需求 在页面端可以在页面不刷新情况下上传多个有大小限制的word文件,并返回文件保存的路径,同时可以删除误上传的文件. 准备 下载该插件 该插件依赖jquery1.9.1版本(其它不清楚)*在jsp页 ...

  10. [b0019] python 归纳 (五)_类装饰器

    总结: 类装饰器, 本质是一个函数,输入一个类,返回一个类 Case 1 啥都没做 def deco(in_class): return in_class @deco class Cat: def _ ...