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. 如何使用re模块进行测试用例的参数化

    import re import os from scripts.handle_mysql import HandleMysql from scripts.handle_config import H ...

  2. tinyriscv---一个从零开始写的极简、易懂的开源RISC-V处理器核

    本项目实现的是一个微riscv处理器核(tinyriscv),用verilog语言编写,只求以最简单.最通俗易懂的方式实现riscv指令的功能,因此没有特意去对代码做任何的优化,因此你会看到里面写的代 ...

  3. Linux域名服务DNS

    什么是 DNS DNS 全称是 Domain Name System,大意是域名解析系统,它的职责是把域名翻译成一个一个可以识别的 IP 供不同的计算机设备连接. linux 有关 DNS 解析的配置 ...

  4. 用vue和layui简单写一个响应式数据展示表

    在创建项目之前,先把我们需要的文件打包处理 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  5. Flutter 你需要知道的那些事 01

    公众号「AndroidTraveler」首发. 1. width 属性 对于设置控件宽度填充父控件这件事情,在 Android 里面,只需要设置 MATCH_PARENT 即可. 但是在 Flutte ...

  6. PHP统计二维数组个数

    count($arr) $arr = [ ['id'=>1,'name'=>'Tom'], ['id'=>2,'name'=>'Sun'], ['id'=>3,'name ...

  7. 重温《NoSQL精粹》

    前言 在网上有关Redis相关文章满天飞的时候,它是什么,用于解决什么问题,有哪些相类似的技术,与传统的关系型数据库有哪些差别,什么时候使用?这个时候我决定重温一下<NoSQL精粹>. 也 ...

  8. Mysql—索引原理与详解

    索引的原理 索引的优点和缺点和使用原则 索引优点: 可以加快数据的检索速度,提高查询速度. 所有的MySql列类型(字段类型)都可以被索引,也就是可以给任意字段建立索引. 全文检索字段进行搜索优化. ...

  9. Tangent element-Vs虚拟调色台安装配置

    iPad安装element-Vs 从Tangent网站下载Element-Vs的Tangent Hub程序安装 确保系统防火墙允许对Tangent Hub的网络访问并且与iPad共用一个局域网连接 开 ...

  10. IAR运行程序警告:undefined behavior: the order of volatile accesses is undefined in this statement

    运算符两边都是volatile变量的警告,将IAR设置如下即可: