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. Spring NoSuchBeanDefinitionException

    转http://www.baeldung.com/spring-nosuchbeandefinitionexception 1. Overview In this article, we are di ...

  2. 20165305 学习基础和C语言基础调查

    学习基础和C语言基础调查 <优秀的教学方法---做教练与做中学>心得 在<优秀的教学方法---做教练与做中学>文章中又一次提到了"做教练"这一学习方法,因为 ...

  3. 35 个最好用的 Vue 开源库

    35 个最好用的 Vue 开源库 Vue.js 是一个非常易用的渐进式 JavaScript 框架,用于构建用户界面. 1.Vue Dark Mode Vue.js 的一个极简主义的深色设计系统.它提 ...

  4. 区块链公链分片技术(sharding)方案,配思维导图

    区块链公链分片技术(sharding)方案,配思维导图 分片技术(sharding)方案 以太坊分片思路 其基本思想是,将网络中的节点分成不同的碎片,各分片可以并行处理不同交易,这样可以并行处理相互之 ...

  5. avr定时器做的正弦波

    2010-04-19 16:53:00 实物照片如下 RC电路的电阻为1K与10K时的波形分别如下 仿真图片如下: 程序如下: #include <iom16v.h> #include & ...

  6. 源码下载:74个Android开发开源项目汇总

    1. ActionBarSherlock ActionBarSherlock应该算得上是GitHub上最火的Android开源项目了,它是一个独立的库,通过一个API和主题,开发者就可以很方便地使用所 ...

  7. 同行span标签设置display:inline-block;overflow:hidden垂直对齐问题

    1 问题描述:一个div包含 三个span 当span2 类样式设置如下图时,将导致垂直方向不对齐的情况 2解决方案: 将前面的也设置同样的样式 overflow:hidden; display:in ...

  8. ubunta_django_install

    sudo apt-get install python-pip sudo apt-get install python-virtualenv #安装本地虚拟环境管理工具 mkdir ~/django ...

  9. printf("loops %u / %u%c[K\n", loops + 1, opts->loops, 27); printf("%cM", 27);

    serialcheck.c中的一段代码一直弄不明白: do { status = stress_test_uart_once(opts, fd, data, data_len); memset(opt ...

  10. 4~20mA电流输出芯片XTR111完整电路(转)

    源: 4~20mA电流输出芯片XTR111完整电路