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错误说明的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  4. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  5. [LeetCode] Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. Openstack的网卡设置

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 最开始接触Openstack,这块是比较头疼的,不同的文档,设置都会有所差异,并 ...

  2. 基于spring-cloud的微服务(4)API网关zuul

    API网关是微服务架构中的很重要的一个部分,内部有多个不同的服务提供给外部来使用,API网关可以对外做统一的入口,也可以在网关上做协议转换,权限控制和请求统计和限流等其他的工作 spring-clou ...

  3. shell脚本技巧记录

    2014/4/9 shell脚本变量处理: ${varible##*string} //从左向右截取最后一个string后的字符串 ${varible#*string} //从左向右截取第一个stri ...

  4. 【BZOJ1210】[HNOI2004]邮递员 插头DP+高精度

    [BZOJ1210][HNOI2004]邮递员 Description Smith在P市的邮政局工作,他每天的工作是从邮局出发,到自己所管辖的所有邮筒取信件,然后带回邮局.他所管辖的邮筒非常巧地排成了 ...

  5. jmeter聚合报告导出时乱码的解决

    在使用jmeter性能测试时,聚合报告导出后使用excel打开时是乱码,查看相关文件后是编码的问题,解决方法如下: 1.现象: 用excel打开变成这种乱码无法看清 2.解决: 先使用记事本打开后,选 ...

  6. 判断String 中文混输 长度

    extends:http://www.tuicool.com/articles/EB36Jv public static int calculateLength(String etString) { ...

  7. NGINX域名跳转案列

    1.不同域名不同路径跳转 nginx实现a.com/teacher域名跳转到b.com/student 若想实现上面题目的跳转,目前鄙人知道两种方式: 1.return 2.proxy_pass 具体 ...

  8. bootstrapValidator remote 的接受 验证 值

    本来之前也做过一次这样的验,但可能是这两天太热脑袋不够用了,于是就只有看看源码咯 that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f ...

  9. poj1269 intersecting lines【计算几何】

    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...

  10. 把本地项目文件上传到github远程仓库的教程

    参考廖雪峰的git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000  非常详 ...