Description

Given an array of integers, remove the duplicate numbers in it.

You should:

  1. Do it in place in the array.
  2. Move the unique numbers to the front of the array.
  3. Return the total number of the unique numbers.

Example 1:

Input:
nums = [1,3,1,4,4,2]
Output:
[1,3,4,2,?,?]
4

Challenge

  1. Do it in O(n) time complexity.
  2. Do it in O(nlogn) time without extra space.

Related Problems

487 Name Deduplication

思路1 Hashmap存放法:

O(n) time, O(n) space

注意:

  1. Hashmap中相同的key在Map中只会有一个与之关联的value存在。如果原本已经存在对应的key,则直接改变对应的value。
  2. 利用上条性质,用map中的key值存储数组元素,再遍历map,打印出的数组就不会有重复项。
  3. 遍历 HashMap 的方法 (line 8/9 的注释是另一种遍历方法)

代码:

 public int deduplication(int[] nums) {
HashMap<Integer, Boolean> mp = new HashMap<>();
for (int i = 0; i < nums.length; i++){
mp.put(nums[i], true);
} int result = 0;
for (Map.Entry<Integer, Boolean> entry : mp.entrySet()) // for (Integer key : mp.keySet())
nums[result++] = entry.getKey(); // nums[result++] = key;
return result;
}

思路2: 双指针法

O(nlogn) time, O(1) extra space

先对数组排序,再用快指针遍历整个数组,慢指针改变数组使其只包含非重复数字。

注意:

快指针放在for循环里。慢指针在for循环外赋值,才能return。

代码:

    public int deduplication(int[] nums) {
if (nums.length == 0) return 0;
Arrays.sort(nums);
int i = 0;
for (int j = 1; j < nums.length; j++){
if (nums[i] != nums[j])
nums[++i] = nums[j];
}
return i+1; }

Lintcode521-Remove Duplicate Numbers in Array-Easy的更多相关文章

  1. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

  2. Leetcode 26. Remove Duplicates from Sorted Array (easy)

    Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...

  3. LeetCode_26. Remove Duplicates from Sorted Array

    26. Remove Duplicates from Sorted Array Easy Given a sorted array nums, remove the duplicates in-pla ...

  4. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  5. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. LeetCode--122、167、169、189、217 Array(Easy)

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  9. LeetCode--1、26、27、35、53 Array(Easy)

      1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to ...

随机推荐

  1. GUI常用对象介绍3

    %text hf = axes; ht = text(,,'示例'); get(ht); %公式 并且设置位置坐标 (积分符号) text('String','\int_0^x dF(x)','Pos ...

  2. flask框架----flask-script组件

    Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...

  3. vue style 的scoped 使用

    1 原理 vue 可以通过在 style标签添加scoped这个属性来控制 组件内的css作用域 <style scoped> @media (min-width: 250px) { .l ...

  4. The Little Prince-12/05

    The Little Prince-12/05 "When a mystery is too overpowering, one dare not disobey. Absurd as it ...

  5. 推荐:全新Java开发思维导图

    蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力.培训.出国.大公司的经历,还有很多很好的朋友. 但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可 ...

  6. 通过 Java 线程堆栈进行性能瓶颈分析

    改善性能意味着用更少的资源做更多的事情.为了利用并发来提高系统性能,我们需要更有效的利用现有的处理器资源,这意味着我们期望使 CPU 尽可能出于忙碌状态(当然,并不是让 CPU 周期出于应付无用计算, ...

  7. OCR技术初识

    一.什么是OCR OCR英文全称是Optical Character Recognition,中文叫做光学字符识别.它是利用光学技术和计算机技术把印在或写在纸上的文字读取出来,并转换成一种计算机能够接 ...

  8. 01:MongoDB基础

    1.1 MongoDB简介 1.特点 1. MongoDB的提供了一个面向文档存储,操作起来比较简单和容易. 2. 你可以在MongoDB记录中设置任何属性的索引 (如:FirstName=" ...

  9. shell脚本一键安装nginx

    依赖包安装包放在一起, 直接执行这个脚本就行. #!/bin/bash #--------------------------------------------------------------- ...

  10. C语言三个结束符:EOF ‘\0’ '\n'