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. dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta

    dyld: Library not loaded: @rpath/XCTest.framework/XCTest   Referenced from: /private/var/mobile/Cont ...

  2. 〖Windows〗zigbee实验之cygwin编译TestSimpleMac出错的解决方法

    1. 错误代码如下: ... C51 COMPILER V8. - SN: K1CMC-IEYCYC COPYRIGHT KEIL ELEKTRONIK GmbH - *** ERROR C141 I ...

  3. 【Docker】安装tomcat并部署应用

    安装tomcat 1.拉取tomcat镜像 docker pull docker.io/tomcat 查看镜像 docker images 2.启动tomcat 首先添加8090端口:firewall ...

  4. Python之reduce

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之reduce #http://python.jobbole.com/82597/ #1)red ...

  5. 身份证查询API

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://apistore.baidu.com/apiworks/servicedetail/113.h ...

  6. C#多线程之 ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

  7. CentOS 6.5上使用gdb调试时出现Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686 .

    在CentOS6.5上用gdb调试时提示Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686先修改 ...

  8. HighCharts/Highstock使用小结,使用汉化及中文帮助文档

       此文档是本人在开发过程图形报表时使用HighCharts所遇到的问题及解决方案 .最后附上有HighCharts中文帮助文档 HighCharts  版本:Highcharts-3.0.1 Hi ...

  9. 转载:substr() mb_substr() mb_subcut区别与联系

    substr() $rest = substr("abcdef", 1); //bcdef $rest = substr("abcdef", 1,5); //b ...

  10. xargs详解

    一.场景 这个命令是错误的 find ./ -perm +700 |ls -l 这样才是正确的 find ./ -perm +700 |xargs ls -l  二.用法 [root@localhos ...