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. plsql注册-转

    注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca https://blo ...

  2. STM32 一个定时器产生4路 独立调频率,占中比可调,脉冲个数可以统计。

    实现这个功能,基本原理是利用STM32 的输出比较功能. 1.其它设置就是普通定时器的设置这里开启,四个输出比较中断,和一个更新中断, 更新中断这里不需要开也可以达到目的,我这里开启是做其它的用处的. ...

  3. java -cp & java jar的区别

    java -cp java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库和jar包,需要全路径到jar包,多个jar包之间连接符:window上分号“;”.Lin ...

  4. 使用Holer将本地端口映射到公网

    What is holer Holerexposes local servers behind NATs and firewalls to the public internet over secur ...

  5. QT多线程简单例子

    在Qt中实现多线程,除了使用全局变量.还可以使用信号/槽机制. 以下例子使用信号/槽机制. 功能: 在主线程A界面上点击按钮,然后对应开起一个线程B.线程B往线程A发送一个字符串,线程A打印出来. 1 ...

  6. NUL和NULL

    此处讨论C语言中的NUL和NULL 1.NUL是ASCII字符集中'\0'字符的名字,它的字节模式为全0 2.NULL指一个其值为0的指针 3.它们都是整型值,其值也相同,所以可以互换使用 4.然而, ...

  7. nginx+php+memcache实现hash一致性memcache 集群

    我们工作中可能会遇到key-value数据库,如果我们面对的不止一台memcache服务器,而是很多台.那么现在就回出现一个问题: 当我们访问nginx服务器的时候,我们会判断memcache中是否有 ...

  8. django自定义错误响应

    在做一个web时,总是会出现各种错误,如400.403.404.500等.一般开发都要做对应的处理,给一些友好提示,或返回一些公益广告等. 在Django中,默认提供了常见的错误处理方式,比如: ha ...

  9. Python+selenium点击网页上指定坐标

    from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains dr = ...

  10. alert.log中的minact-scn: useg scan erroring out with error e:376警告

    早上,开发说昨晚一台服务器的undo好像有异常,早上上去一看,库停了,正常startup之后,随手crud了一把,都正常.去看alert.log日志,发现undo某个数据块访问报I/O读错误,如下: ...