LintCode 521.去除重复元素
LintCode 521.去除重复元素
描述
给一个整数数组,去除重复的元素。
你应该做这些事
1.在原数组上操作
2.将去除重复之后的元素放在数组的开头
3.返回去除重复元素之后的元素个数
挑战
1.O(n)时间复杂度.
2.O(nlogn)时间复杂度但没有额外空间
答案
使用Map存储。时间复杂度O(n),空间复杂度O(n)
public int deduplication(int[] nums) {
// write your code here
HashMap<Integer, Integer> map = new HashMap<>();
int i = 0, j = nums.length - 1;
while (i <= j) {
if (map.get(nums[i]) == null) {
map.put(nums[i], 1);
} else {
map.put(nums[i], map.get(nums[i]) + 1);
}
if (map.get(nums[i]) > 1) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j--;
} else {
i++;
}
}
return map.size();
}
排序后用双指针
public int deduplication(int[] nums) {
Arrays.sort(nums); int i = 0, j = 1;
while (j < nums.length) {
if(nums[j] != nums[i]) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
j++;
}
return i+1;
}
LintCode 521.去除重复元素的更多相关文章
- Python列表去除重复元素
主要尝试了3种列表去除重复元素 #2.去除列表中的重复元素 #set方法 def removeDuplicates_set(nums): l2 = list(set(l1)) #用l1的顺序排序l2 ...
- java集合 collection-list-ArrayList 将自定义对象作为元素存到ArrayList集合中,并去除重复元素。
import java.util.*; /* 将自定义对象作为元素存到ArrayList集合中,并去除重复元素. 比如:存人对象.同姓名同年龄,视为同一个人.为重复元素. 思路: 1,对人描述,将数据 ...
- php将多个值的数组去除重复元素
array_unique(array) 只能处理value只有单个的数组. 去除有多个value数组,可以使用如下函数实现: function more_array_unique($arr=array ...
- ArrayList去除重复元素(多种方法实现)
package other; import java.util.ArrayList; import java.util.HashSet; public class test4 { public sta ...
- [C++]vector去除重复元素
#include <iostream> #include <vector> #include <algorithm> #include <set> us ...
- C语言两个升序递增链表逆序合并为一个降序递减链表,并去除重复元素
#include"stdafx.h" #include<stdlib.h> #define LEN sizeof(struct student) struct stud ...
- ArrayList去除重复元素(包括字符串和自定义对象)
1.去除重复字符串 package com.online.msym; import java.util.ArrayList; import java.util.Iterator; @SuppressW ...
- 使用HashSet<>去除重复元素的集合
比如,某一个阵列中,有重复的元素,我们想去除重复的,保留一个.HashSet<T>含不重复项的无序列表,从MSDN网上了解到,这集合基于散列值,插入元素的操作非常快. 你可以写一个方法: ...
- ArrayList去除重复元素
去除一个ArrayList的重复元素有两种方法:(ArrayList与Vector的存储结构是Object[],LinkedList是双向列表) 第一种是不需要借助临时list,用equals方法比较 ...
随机推荐
- 1840: Jack Straws
1840: Jack Straws 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte 总提交: 168 测试通过:129 描述 I ...
- python转换图片格式
在图片所在的路径下,打开命令窗口 bmeps -c picturename.png picturename.eps
- windows red5相关
red5部署 前段时间把red5服务器搭建好了,现在记录下是如何搭建的.1,下载对应版本的red5https://github.com/Red5/red5-server/releases2,如果没有安 ...
- 20190423 PowerDesigner 数据库模型快速建立
后面我在做一个视频的讲解记录吧! 那种讲解记录,只是为了演示按钮功能在什么地方,这个功能的作用是什么 这个软件相对比较简单的使用步骤,主要有三步 第一. 选择好你针对的数据库版本和类型创建数据库名称基 ...
- ADB——应用交互
使用ADB与手机应用交互 应用交互的操作包括:启动应用 / 调起Activity.调起Services.停止Service.发送广播.强行停止应用 基本命令 am <command> 常用 ...
- [PCL]模型拟合方法——随机采样一致性
SACSegmentation封装了多种Ransac方法,包括: RandomSampleConsensus, LeastMedianSquares, MEstimatorSampleConsensu ...
- _sntprintf_s 和 _sntprintf 区别
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-s-snprintf-s-l-snwprintf-s ...
- unity开发多语言版本
1.文字部分 ①.文字提取参考 http://www.xuanyusong.com/archives/2987: ②.把提取出来的文字放到excel总转换成繁体等版本: ③.把excel转换成txt文 ...
- CDI services--Decorators(装饰器)
1.Decorators装饰器综述 拦截器是一种强大的方法在应用程序捕捉运行方法和解耦.拦截器可以拦截任何java类型的调用. 这使得拦截器适合解决事务管理,安全性,以及日记记录. 本质上说,拦截器并 ...
- Leetcode: Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...