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. Log4J基础详解及示例大全(转)

    log4j可以通过使用配置文件的方式进行配置. 配置步骤如下: 1.定义日志组件logger 每个logger都可以拥有一个或者多个appender,每个appender表示一个日志的输出目的地,比如 ...

  2. 【函数封装】javascript判断移动端操作系统为android 或 ios 或 iphoneX

    function isPhone(){ var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.index ...

  3. Git HEAD detached from XXX (git HEAD 游离) 解决办法

    本文 Git 图片主要来自:图解 Git,非常感谢! 读完本文你将了解: 什么是 HEAD HEAD 游离状态的利与弊 具体解决操作 Thanks 什么是 HEAD Git 中的 HEAD 可以理解为 ...

  4. Step4:SQL Server 跨网段(跨机房)复制

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搭建过程(Process) 注意事项(Attention) 参考 ...

  5. spark机器学习笔记01

     1)外部数据源 val distFile1 = sc.textFile("data.txt") //本地当前目录下文件 val distFile2 =sc.textFile(& ...

  6. PHP5.4以下的json_encode中文被转码的问题

    PHP的json_encode中文被转码的问题   在php5.2中做json_encode的时候.中文会被unicode编码, php5.3加入了options参数, 5.4以后才加入JSON_UN ...

  7. Python建立多线程任务并获取每个线程返回值

    1.进程和线程 (1)进程是一个执行中的程序.每个进程都拥有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.进程也可以派生新的进程来执行其他任务,不过每个新进程都拥有自己的内存和数据栈,所 ...

  8. OSI七层协议与TCP/IP模型

    OSI为Open System Interconnection的缩写,意为开放式系统互联,国际标准化组织(ISO,International Organization for Standardizat ...

  9. rabbitmq集群安装与配置(故障恢复)

    0.首先按照http://www.cnblogs.com/zhjh256/p/5922562.html在至少两个节点安装好(不建议单机,没什么意义) 1.先了解rabbitmq集群架构,http:// ...

  10. 【题解】bzoj 4478 [Jsoi2013]侦探jyy

    原题传送门 弱智搜索题 我们就枚举每个点,先判断它是否必须发生,如果没有必须发生,开始搜索它的祖先,如果祖先中有必须发生的,那么它就必须发生,如果祖先中没有必须发生的,那么搜索所有入度为0的点(除了它 ...