Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in O(n) complexity.
Best Solution: only scan one direction,
If the number x is the start of a streak (i.e., x-1 is not in the set), then test y = x+1, x+2, x+3, ... and stop at the first number y not in the set. The length of the streak is then simply y-x and we update our global best with that. Since we check each streak only once, this is overall O(n).
public class Solution {
/**
* @param nums: A list of integers
* @return an integer
*/
public int longestConsecutive(int[] num) {
// write you code here
HashSet<Integer> set = new HashSet<Integer>();
for (int elem : num) {
set.add(elem);
}
int longestLen = 0;
for (int n : set) {
if (!set.contains(n-1)) {
int len = 0;
while (set.contains(n)) {
len++;
n++;
}
longestLen = Math.max(longestLen, len);
}
}
return longestLen;
}
}
Union Find (optional), just to know that there's this solution
class Solution {
public int longestConsecutive(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
unionFind uf = new unionFind(nums.length);
for (int i = 0; i < nums.length; i ++) {
uf.fathers[i] = i;
uf.count ++;
if (map.containsKey(nums[i])) continue;
map.put(nums[i], i);
if (map.containsKey(nums[i] - 1)) {
uf.union(i, map.get(nums[i] - 1));
}
if (map.containsKey(nums[i] + 1)) {
uf.union(i, map.get(nums[i] + 1));
}
}
return uf.maxUnion();
}
public class unionFind {
int[] fathers;
int count;
public unionFind(int num) {
this.fathers = new int[num];
Arrays.fill(this.fathers, -1);
this.count = 0;
}
public void union(int i, int j) {
if (isConnected(i, j)) return;
int iRoot = find(i);
int jRoot = find(j);
fathers[iRoot] = jRoot;
this.count --;
}
public int find(int i) {
while (fathers[i] != i) {
i = fathers[i];
}
return i;
}
public boolean isConnected(int i, int j) {
return find(i) == find(j);
}
// returns the maxium size of union
public int maxUnion(){ // O(n)
int[] count = new int[fathers.length];
int max = 0;
for(int i=0; i<fathers.length; i++){
count[find(i)] ++;
max = Math.max(max, count[find(i)]);
}
return max;
}
}
}
Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明的更多相关文章
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- C语言程序设计--宏和预处理
C语言宏 宏定义常量 #include <stdio.h> #define SIZE 100 #define BANNER "WARNING:" int main(vo ...
- C语言位操作--逻辑运算符组合
假设读者熟悉普通代数与布尔代数,下面是部分常见的涉及到加法.减法与逻辑运算符的组合: a. -x=~x+1 b. =~(x-1) c. ~x=-x-1 ...
- 【CF896E】Welcome home, Chtholly 暴力+分块+链表
[CF896E]Welcome home, Chtholly 题意:一个长度为n的序列ai,让你支持两种操作: 1.l r x:将[l,r]中ai>x的ai都减去x.2.l r x:询问[l,r ...
- Android 国内集成使用谷歌地图
extends:http://blog.csdn.net/qduningning/article/details/44778751 由于众做周知的原因在国内使用谷歌地图不太方便,在开发中如果直接使用会 ...
- vue--引入富文本编辑器
https://blog.csdn.net/div_ma/article/details/79536634 // 使用 https://blog.csdn.net/div_ma/article/det ...
- 9.20Ajax知识sweetalet
2018-9-20 14:19:55 2018-9-20 21:33:05 周末可以帮我图书商城再次优化一下!! 加入 Ajax请求,,再加上 sweetAlert 甜蜜对话框插件! 要是再加上模态 ...
- Nginx设置网站维护页面
网站升级需要停服,可以在Nginx设置静态页面设置强制跳转 修改nginx配置文件nginx.conf http { sendfile on; keepalive_timeout 65; server ...
- POJ 2406 - Power Strings - [KMP求最小循环节]
题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...
- 对position的认知观
position :absolute 认识以前有的理解不正确,以为没有设置left,top 与设置left :0,top : 0是一样的,现在认识为,错误! 没有设置的时候,该absolute元素{由 ...
- Lost connection to MySQL server during query ([Errno 104] Connection reset by peer)
Can't connect to MySQL server Lost connection to MySQL server during query · Issue #269 · PyMySQL/Py ...