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 ...
随机推荐
- Inferred type 'S' for type parameter 'S' is not within its bound;
在使用springboot 方法报错: Inferred type 'S' for type parameter 'S' is not within its bound; should extends ...
- orm操作
一.必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> get(**kwar ...
- mysql01
CREATE TABLE `student`( `studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号', `loginPwd` VARCHAR(20 ...
- STM32硬件IIC (转)
源: STM32硬件IIC
- php 加密 解密 密码传输
php 加密 解密 密码传输 <?php /* * * 使用按位异或运算 加密 * $str 明文 * $salt 盐 * */ public static function xor_encry ...
- php 接收blob数据流,base64数据流 转为 blob二进制数据流
php正常接收参数的方式如下:$_GET$_POST$_REQUEST 但是如果跨语言接收请求参数的话,可能会出现一系列的问题,其他语言的http请求可能是基于数据流的概念来传递参数的,如果按照常规处 ...
- Net中应用 Redis 扩展类
GIt地址:https://gitee.com/loogn/stackexchange-redis-typedextensions 1.stackexchange 类调用 using System; ...
- Python爬虫(二)——豆瓣图书决策树构建
前文参考: https://www.cnblogs.com/LexMoon/p/douban1.html Matplotlib绘制决策树代码: # coding=utf-8 import matpl ...
- [c/c++] programming之路(2)、kill QQ,弹出系统对话框,吃内存等
一.删除文件 二.盗取密码的原理 #include<stdlib.h> //杀掉QQ,然后提示网络故障,请重新登陆,弹出高仿界面,获取账号密码,然后打开QQ进行登录 void main() ...
- kali linux Burp Suite使用教程
设置Firefox并配置代理 配置Firefox Burp Suite包含拦截代理. 要使用Burp Suite,您必须配置浏览器以通过Burp Suite代理传递其流量. 这对于Firefox来说并 ...