LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence

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.
SOLUTION1:
用HashMap来空间换时间.
1. 在map中创建一些集合来表示连续的空间。比如,如果有[3,4,5]这样的一个集合,我们表示为key:3, value:5和key:5, value3两个集合,并且把这2个放在hashmap中。这样我们可以在O(1)的时间查询某个数字开头和结尾的集合。
2. 来了一个新的数字时,比如:N=6,我们可以搜索以N-1结尾 以N+1开头的集合有没有存在。从1中可以看到,key:5是存在的,这样我们可以删除3,5和5,3这两个key-value对,同样我们要查以7起头的集合有没有存在,同样可以删除以7起始的集合。删除后我们可以更新left,right的值,也就是合并和扩大集合。
3. 合并以上这些集合,创建一个以新的left,right作为开头,结尾的集合,分别以left, right作为key存储在map中。并且更新max (表示最长连续集合)
public class Solution {
public int longestConsecutive(int[] num) {
if (num == null) {
return 0;
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int max = 0;
int len = num.length;
for (int i = 0; i < len ; i++) {
// 寻找以num[i] 起头或是结尾的,如果找到,则可以跳过,因为我们
// 不需要重复的数字
if (map.get(num[i]) != null) {
continue;
}
int left = num[i];
int right = num[i];
// 寻找左边界
Integer board = map.get(num[i] - 1);
if (board != null && board < left) {
// 更新左边界
left = board;
// 删除左边2个集合
map.remove(left);
map.remove(num[i] - 1);
}
// 寻找右边界
board = map.get(num[i] + 1);
if (board != null && board > right) {
// 更新右边界
right = board;
// 删除右边2个集合
map.remove(right);
map.remove(num[i] + 1);
}
// 创建新的合并之后的集合
map.put(left, right);
map.put(right, left);
max = Math.max(max, right - left + 1);
}
return max;
}
}
SOLUTION2:
引自大神的解法:http://blog.csdn.net/fightforyourdream/article/details/15024861
我们可以把所有的数字放在hashset中,来一个数字后,取出HashSet中的某一元素x,找x-1,x-2....x+1,x+2...是否也在set里。
// solution 2: use hashset.
public int longestConsecutive(int[] num) {
if (num == null) {
return 0;
} HashSet<Integer> set = new HashSet<Integer>();
for (int i: num) {
set.add(i);
} int max = 0;
for (int i: num) {
int cnt = 1;
set.remove(i); int tmp = i - 1;
while (set.contains(tmp)) {
set.remove(tmp);
cnt++;
tmp--;
} tmp = i + 1;
while (set.contains(tmp)) {
set.remove(tmp);
cnt++;
tmp++;
} max = Math.max(max, cnt);
} return max;
}
2015.1.2 redo:
public class Solution {
public int longestConsecutive(int[] num) {
if (num == null) {
return 0;
}
HashSet<Integer> set = new HashSet<Integer>();
for (int i: num) {
set.add(i);
}
int max = 0;
for (int i: num) {
set.remove(i);
int sum = 1;
int tmp = i - 1;
while (set.contains(tmp)) {
// bug 1:forget to add the remove statement.
set.remove(tmp);
sum++;
tmp--;
}
tmp = i + 1;
while (set.contains(tmp)) {
set.remove(tmp);
sum++;
tmp++;
}
max = Math.max(max, sum);
}
return max;
}
}
GITHUB:
LeetCode: Longest Consecutive Sequence 解题报告的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 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 @ 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 && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 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 ...
随机推荐
- Openerp 7.0消息推送
在一个文档的state变化时,需要将变化情况告知关注用户,通过研究account.invoice的代码,发现是经过如下过程实现此功能的: 1.添加一个消息阶段: <record id=" ...
- Java Method Area
ref http://blog.csdn.net/huangfan322/article/details/53220169 https://docs.oracle.com/javase/specs/j ...
- eclipse 修改maven项目的jdk版本
eclipse 修改maven项目的jdk版本 CreationTime--2018年6月8日10点29分 Author:Marydon 1.情景展示 jdk版本太低,如何修改 2.错误方式 第一 ...
- python之函数用法iter()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法iter() #iter() #说明:对一个对象调用 iter() 就可以得到它的迭代 ...
- 学习hibernate笔记
曾经学习java的时候,一開始就学习了hibernate,那时候总认为ssh很高大上,所以就急忙看了下相关视频.只是由于实际须要不高,所以后来一直没有使用上hibernate组件.如今一年过去了,也疯 ...
- 如何在Win8中设置虚拟热点共享上网(转)
摘自:http://www.enet.com.cn/article/2013/0408/A20130408273749.shtml 在Windows 7中,我们可以通过网络与共享中心的“设置新的连接和 ...
- 配置Eclipse 3.3 + tomcat 6.0 + lomboz 3.3进行Web开发
http://www.cnblogs.com/xtsong/articles/1203155.html我以前编程用的都是Eclipse 3.2,这次跑到www.eclipse.org上去溜达了一番,发 ...
- 使用SecureCRT连接linux
1.登录之后进入linux系统,输入ifconfig(interfaces config)查看网卡信息 2.设置VMWare的虚拟机连接方式为仅主机模式 3.查看VMWare为仅主机模式虚拟网卡IP地 ...
- JFinal连接数据库配置说明
本文采用的是加载配置文件的形式和数据库进行交互 ps:数据库采用的是postgresql 1.加载配置文件 public void configConstant(Constants me) { Pro ...
- 【Linux】目录配置
为什么每套Linux distributions的配置文件.执行文件.每个目录内放置的文件其实都差不多?因为有一套需要依据的标准!我们底下就来瞧一瞧. 因为利用Linux来开发产品或distribut ...