Lintcode521-Remove Duplicate Numbers in Array-Easy
Description
Given an array of integers, remove the duplicate numbers in it.
You should:
- Do it in place in the array.
- Move the unique numbers to the front of the array.
- 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
- Do it in O(n) time complexity.
- Do it in O(nlogn) time without extra space.
Related Problems
487 Name Deduplication
思路1 Hashmap存放法:
O(n) time, O(n) space
注意:
- Hashmap中相同的key在Map中只会有一个与之关联的value存在。如果原本已经存在对应的key,则直接改变对应的value。
- 利用上条性质,用map中的key值存储数组元素,再遍历map,打印出的数组就不会有重复项。
- 遍历 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的更多相关文章
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- 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 ...
- LeetCode_26. Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted Array Easy Given a sorted array nums, remove the duplicates in-pla ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 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 ...
- 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 ...
随机推荐
- sqlyog下载
sqlyog下载(附注册码):http://www.onlinedown.net/soft/24926.htm
- 转:判断Caps Lock键是否打开,如果打开则关闭
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- bzoj2287 [POJ Challenge]消失之物
题目链接 少打个else 调半天QAQ 重点在47行,比较妙 #include<algorithm> #include<iostream> #include<cstdli ...
- [转载]FileStream读写文件
FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...
- AngularJS的简单入门
AngularJS诞生于2009年,由Misko Hevery等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心 ...
- org.springframework.jdbc.UncategorizedSQLException
org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException fo ...
- shell 获取随机字符串
一直使用 /dev/urandom 和md5sum的方式去随机字符串,感觉还是不够随机,毕竟只有小写字母和数字嘛. 换换口味: [root@localhost ~]# arr=(`echo {a..z ...
- Docker学习笔记之搭建 Java Web 项目运行环境
0x00 概述 Java Web 泛指以 Java 程序为基础向外提供 Web 服务的技术及相关工具,狭义上来说,我们也可以说 Java Web 是由 Servlet 程序提供的 Web 服务. 对我 ...
- mysql查询INFORMATION_SCHEMA表很慢的性能优化
最近发现,我们有些环境的tomcat应用启动非常缓慢,大部分在3-5分钟,有个测试环境更加阶段,要十几分钟才能启动完成.经过仔细分析,是一个查询INFORMATION_SCHEMA库中数据字典信息的查 ...
- PO VO BO DTO POJO DAO之间的关系
J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了. PO:persistant ...