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:

LongestConsecutive.java

LeetCode: Longest Consecutive Sequence 解题报告的更多相关文章

  1. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

  2. LeetCode——Longest Consecutive Sequence

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

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

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

  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 && Summary: Iterator用法以及ConcurrentModificationException错误说明

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

  8. LeetCode—Longest Consecutive Sequence

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

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

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

随机推荐

  1. c#委托是什么?事件是不是一种委托?

    C#的委托是CTS(公共类型系统)规定的5中类型之一(类类型.结构类型.接口类型.枚举类型.委托类型).它类似于c或c++中的函数的指针,但函数指针只能引用静态方法,而委托既能引用静态方法,也能引用实 ...

  2. office-word去掉效验红色的波浪线

         工作中,总是能发现不足.能再次学习到知识和经验!

  3. 【微信小程序】:客服消息教程

    1.本教程完全链接W3Cschool的教程,已经讲的非常清晰和透彻. 2.链接:https://www.w3cschool.cn/weixinapp/weixinapp-api-custommsg-c ...

  4. BIND9源码分析之UDP数据处理

    本文简要介绍一下BIND9中的UDP数据处理,包括如何创建socket.设置什么socket参数.多线程环境中如何让多个线程读取53端口的数据等等. BIND9的架构采用event-driven和ta ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  6. 堆 C语言实现

    1.基本概念 堆分为小根堆和大根堆,对于一个小根堆,它是具有如下特性的一棵完全二叉树: (1)若树根结点存在左孩子或右孩子,则根结点的值(或某个域的值)小于等于左右孩子结点的值(或某个域的值) (2) ...

  7. 超酷的Android 侧滑(双向滑动菜单)效果

    下面看看我们如何使用它,达到我们想要的效果 public class MainActivity extends Activity { /** * 双向滑动菜单布局 */ private SliderM ...

  8. go 学习 ---package

    1.包简述 GO本身没有项目的概念,只有包,包括可执行包和不可执行包,而不管什么包,都应该包含在 $GOPATH/src 目录下,GO命令和编译器会在 $GOPATH/src 目录下搜索相应的包.比如 ...

  9. Jmete ----r默认报告优化

    转自:http://www.cnblogs.com/puresoul/p/5053035.html 一.本文目的: 之前写了两篇文章搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)和A ...

  10. Nodejs 使用 Chrome DevTools 调试 --inspect-brk

    参考链接: https://cnodejs.org/topic/5a9661ff71327bb413bbff5b https://github.com/nswbmw/node-in-debugging ...