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 ...
随机推荐
- C# 调整控件的Z顺序
当窗口或者容器控件中的控件在布局过程中发生重叠的时候,会出现层次性.Z顺序较大的控件会遮挡Z顺序较小的控件,放在顶层的控件会挡住放在底层的控件. 1.编辑一个这样的窗口(使用Label控件) 2.添加 ...
- django项目----函数和方法的区别
一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 举例说明: class Foo(object): def __init__( ...
- JavaUtil smtp 邮件发送
需要用到的jar包:javax.mail.jar package com.lee.util; import java.io.UnsupportedEncodingException; import j ...
- MediaCodec在Android视频硬解码组件的应用
https://yq.aliyun.com/articles/632892 云栖社区> 博客列表> 正文 MediaCodec在Android视频硬解码组件的应用 cheenc 201 ...
- ES6知识整理(4)--数组的扩展
最近工作比较忙,基本每天都会加班到很晚.处理一些客户端兼容问题以及提升用户体验的优化.也将近一周没更文了,现在继续es6的学习总结. 上篇回顾 ES6知识整理(三)--函数的扩展 扩展运算符 形式是3 ...
- 合并两个JsonArray
//合并两个JSONArray public static String joinJSONArray(JSONArray mData, JSONArray array) { StringBuffer ...
- Vue 基础精讲
Vue 基础精讲 Vue 官方文档:https://cn.vuejs.org/v2/guide/ VUE 实例 每个组件都是一个 vue 的实例. 一个 vue 项目是由实例组成的. vue 实例上有 ...
- Linux和windows之间使用scp无密码传输文件,脚本自动化
1.环境 windows2008 R2 和rhel 6.5 2.需求 通过在windows上指定计划任务,自动完成从Linux系统上备份文件到windows 3.工具 win: SSH Secure ...
- noip 2013 提高组 day1
1.转圈游戏: 解析部分略,快速幂就可以过 Code: #include<iostream> #include<fstream> using namespace std; if ...
- linux --- 2.常用命令 , python3, django安装
一.常用命令 1.常识命令 ① w 显示终端连接数 ②pwd 我在哪 ③whoami 我是谁 ④which 命令 找到命令的绝对路径 2.linux 命令行的组 ...