详细见:leetcode.com/problems/longest-consecutive-sequence
Java Solution: github
package leetcode;
import java.util.HashMap;
import java.util.HashSet;
/*
* 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.
*/
/**
* @author zxwtry
* @email zxwtry@qq.com
* @project OJ
* @package leetcode
* @file P128_LongestConsecutiveSequence.java
* @type P128_LongestConsecutiveSequence
* @date 2017年5月9日 下午10:04:45
* @details Solution: AC 86ms 1.69%
* @details Solution2: AC 15ms 62.80%
*/
public class P128_LongestConsecutiveSequence {
public static void main(String[] args) {
int[] n = {1, 2, 3, 4, 5, 6, 8};
new Solution2().longestConsecutive(n);
}
static class Solution {
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
HashSet<Integer> set = new HashSet<>();
for (int n : nums) {
max = Math.max(max, n);
min = Math.min(min, n);
set.add(n);
}
if (max - min == nums.length - 1 && set.size() == nums.length) {
return nums.length;
}
int ans = 0;
for (int n : nums) {
int l = n, r = n;
while (set.contains(-- l)) {
}
while (set.contains(++ r)) {
}
ans = Math.max(ans, r - l - 1);
}
return ans;
}
}
static class Solution2 {
public int longestConsecutive(int[] n) {
int nn = n == null ? 0 :n.length;
if (nn == 0) return 0;
HashMap<Integer, Integer> m = new HashMap<>();
int ans = 0;
for (int v : n) {
if (! m.containsKey(v)) {
int l = m.getOrDefault(v-1, 0);
int r = m.getOrDefault(v+1, 0);
int sum = l + r + 1;
ans = Math.max(ans, sum);
m.put(v, sum);
m.put(v - l, sum);
m.put(v + r, sum);
}
}
System.out.println(m);
return ans;
}
}
}
C Solution: github
#pragma warning(disable:4786)
/*
url: leetcode.com/problems/longest-consecutive-sequence
Solution: AC 113ms 0.61%
Solution2: AC 16ms 10.02%
*/
#include <iostream>
#include <set>
#include <vector>
#include <map>
using namespace std;
class Solution {
public :
int _max(int a, int b) {
return a < b ? b : a;
}
int _min(int a, int b) {
return a < b ? a : b;
}
int longestConsecutive(vector<int >& n) {
int nn = n.size(), i = 0;
int max_val = INT_MIN, min_val = INT_MAX;
set<int > s;
for (i = 0; i < nn; i ++) {
max_val = _max(max_val, n[i]);
min_val = _min(min_val, n[i]);
s.insert(n[i]);
}
if (max_val - min_val == nn-1 && s.size() == nn) return nn;
int ans = 0, l = 0, r = 0;
for (i = 0; i < nn; i ++) {
l = n[i];
r = l;
while (s.count(-- l)) {};
while (s.count(++ r)) {};
ans = _max(ans, r - l - 1);
}
return ans;
}
};
class Solution2 {
public :
int _max(int a, int b) {
return a < b ? b : a;
}
int _min(int a, int b) {
return a < b ? a : b;
}
int longestConsecutive(vector<int >& n) {
int ans = 0, i = 0, nn = n.size();
int v = 0, l = 0, r = 0, sum = 0;
map<int, int > m ;
for (i = 0;i < nn; i ++) {
v = n[i];
if (! m.count(v)) {
l = m.count(v-1) ? m[v-1] : 0;
r = m.count(v+1) ? m[v+1] : 0;
sum = l + r + 1;
m[v] = sum;
ans = _max(ans, sum);
m[v-l] = sum;
m[v+r] = sum;
}
}
return ans;
}
};
int main() {
int a[] = {9, 2, 3, 4, 5, 6};
int an = 6;
vector<int > n;
int i = 0;
for (i = 0; i < an; i ++)
n.push_back(a[i]);
cout<<Solution2().longestConsecutive(n)<<endl;
return 0;
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/longest-consecutive-sequence
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月12日
@details: Solution: 95ms 10.93%
'''
class Solution(object):
def longestConsecutive(self, n):
"""
:type n: List[int]
:rtype: int
"""
nn = 0 if n == None else len(n)
if nn == 0: return 0
m, ans = {}, 0
for v in n:
if not v in m:
l = m[v-1] if v-1 in m else 0
r = m[v+1] if v+1 in m else 0
s = l + r + 1
ans = max(ans, s)
m[v] = s
m[v-l] = s
m[v+r] = s
return ans
if __name__ == "__main__":
n = [1, 2, 3, 4]
print(Solution().longestConsecutive(n))