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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- [转载]几个有趣的Linux命令
本文给大家介绍几个有趣的Linux命令. 1. pv 命令 有时候我们在电影屏幕上看到一些字幕一个个匀速显示出来,像有人在边敲键盘,边显示一样.Linux上的pv命令可以实现这种效果. 默认情况下 ...
- 【读书笔记】socket描述符选项[SOL_SOCKET]
#include <sys/socket.h> int setsockopt( int socket, int level, int option_name, ...
- 在eclipse pydev中使用PEP8来规范python代码
最近在写接口自动话时,代码要上研发的PEP8检查,所以没办法,在pydev中配置如下: 1.打开PEP检查 在pydev 2.3.0之后的版本都自带了pep8检查,但默认是没有开启的 Windows ...
- Xcode快速排错
EXTENDS:http://blog.csdn.net/guo_hongjun1611/article/details/8063009 1,模拟器运行完全没问题,真机运行失败. 有时候我们在模拟器上 ...
- iOS - 开发一套代码多个app展示不同图标和名称
引言 公司项目重构之后,有了相对比较完善的开发体系,首先git分支分为日常.预发.生产三个主要分支,开发阶段都在日常(daily)分支下开相应功能的feature分支,开发完再合并. 我的iOS工程需 ...
- /etc/vim/vimrc的一个的配置
(转)Vim 配置文件===/etc/vimrc "===================================================================== ...
- 9.7 Django 书单列表页面
昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...
- ubuntu-server-18.04 设置开机启动脚本
ubuntu-16.10 开始不再使用initd管理系统,改用systemd systemd is now used for user sessions. System sessions had al ...
- Python的一个命名空间冲突,关于from-import机制
from os import * #import os def foo(): a = listdir("trainingDigits") b = open("traini ...
- TensorFlow 的softmax实例理解
对于理论,简单的去看一下百度上的说明,这里直接上实例,帮助理解. # softmax函数,将向量映射到0-1的范围内,P=exp(ax)/(sum(exp(a1x)+exp(a2x)+...)) in ...