最长连续序列

给定一个未排序的整数数组,找出最长连续序列的长度。

说明

要求你的算法复杂度为O(n)

样例

给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4],返回所求长度 4

解题

排序后比较简单,快排O(nlogn)

后面只需要O(n)的时间复杂度求解了

发现原数组里面有重复数字的时候,下面方法行不通了。

public class Solution {
/**
* @param nums: A list of integers
* @return an integer
*/
public int longestConsecutive(int[] num) {
// write you code here
quickSort(num,0,num.length - 1);
int maxLen = 1;
int subLen = 1;
if(num.length ==4){
for(int i = 0;i< num.length;i++){
System.out.print(num[i] + "\t");
}
}
for(int i = 0;i<= num.length-2 ;i++){
if(num[i] + 1 ==num[i+1]){
subLen++;
}else{
subLen = 1; }
maxLen = Math.max(maxLen,subLen);
}
return maxLen;
} public void quickSort(int[] A,int low ,int high){
if(low>= high)
return;
int i = low;
int j = high;
int tmp = A[low];
while(i<j){
while(i<j && A[j] > tmp)
j--;
if(i<j){
A[i] = A[j];
i++;
} while(i<j && A[i]<= tmp)
i++;
if(i<j){
A[j] = A[i];
j--;
} }
A[i] = tmp;
quickSort(A,low,i-1);
quickSort(A,i+1,high);
}
}

稍作修改,对连续相等的说长度不变

public class Solution {
/**
* @param nums: A list of integers
* @return an integer
*/
public int longestConsecutive(int[] num) {
// write you code here
quickSort(num,0,num.length - 1);
int maxLen = 1;
int subLen = 1;
// if(num.length ==4){
// for(int i = 0;i< num.length;i++){
// System.out.print(num[i] + "\t");
// }
// }
for(int i = 0;i<= num.length-2 ;i++){
if(num[i] + 1 ==num[i+1]){
subLen++;
}else if(num[i]==num[i+1]){
// 相等的时候不做处理
}else{
subLen = 1; }
maxLen = Math.max(maxLen,subLen);
}
return maxLen;
} public void quickSort(int[] A,int low ,int high){
if(low <0 || high>A.length || low>= high)
return;
int i = low;
int j = high;
int tmp = A[low];
while(i<j){
while(i<j && A[j] > tmp)
j--;
if(i<j){
A[i] = A[j];
i++;
} while(i<j && A[i]<= tmp)
i++;
if(i<j){
A[j] = A[i];
j--;
} }
A[i] = tmp;
quickSort(A,low,i-1);
quickSort(A,i+1,high);
}
}

但是96% 数据通过测试时,出现了快排的栈溢出

这个数据量是一万,同时还是完全逆序,网上的解释就是快排数据量太多了。

programcreek 上的方法

定义一个集合,去除了重复数,在每个数两边找,判断是否连续

public class Solution {
/**
* @param nums: A list of integers
* @return an integer
*/
public int longestConsecutive(int[] num) {
// write you code here
if(num.length <= 1)
return num.length;
Set<Integer> set = new HashSet<Integer>();
for(int e:num)
set.add(e);
int max = 1;
for(int e:num){
int count = 1;
int left = e - 1;
int right = e + 1;
while(set.contains(left)){
count++;
set.remove(left);
left--;
}
while(set.contains(right)){
count++;
set.remove(right);
right++;
}
max = Math.max(max,count);
}
return max;
}
}

Python 集合更方便

class Solution:
"""
@param num, a list of integer
@return an integer
"""
def longestConsecutive(self, num):
# write your code here
s = set(num)
Max = 1
for e in num:
count = 1
left = e - 1
right = e + 1
while left in s:
count+=1
s.remove(left)
left -=1
while right in s:
count+=1
s.remove(right)
right+=1
Max = max(Max,count)
return Max

lintcode: 最长连续序列的更多相关文章

  1. [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III

    Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...

  2. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  6. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  7. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

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

  8. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [leetcode-128] 最长连续序列

    给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...

随机推荐

  1. WordPress实现长篇文章/日志/单页面分页功能效果

    在WordPress里写文章,如果内容很多,你可能想要把文章分成几页来让访客浏览,这样既保持了网页的美观,也提高了网页的打开速度.但是在WordPress默认提供的按钮里,你可能找不到文章分页功能所对 ...

  2. Ubuntu 关闭锁屏界面的 on-screen keyboard

    试了试屏幕键盘,在 系统设置里开启了,又关了,但是在屏幕解锁时总是出现 screen keyboard,老烦人了,不知到在哪里关闭了,系统设置里面都关了,网上搜了解决办法,原来在这里 把 show w ...

  3. 邻接表实现Dijkstra算法以及DFS与BFS算法

    //============================================================================ // Name : ListDijkstr ...

  4. HTML5就是现在:深入了解Polyfills

    http://blog.csdn.net/wang16510/article/details/8960312 https://github.com/Modernizr/Modernizr/wiki/H ...

  5. 封装getByClass

    方法一:(普通版),获取单一的class: function getByClass(oParent, sClass) { var aResult = []; var aEle = oParent.ge ...

  6. 【Binary Tree Zigzag Level Order Traversal】cpp

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

  7. hibernate---CRUD

    delete @Test public void testDelete() { Teacher t = new Teacher(); t.setName("t1"); t.setT ...

  8. Matlab实现网络拓补图

    顶点号 顶点号 权值 1 2 400 1 3 450 2 4 300 2 8 230 2 9 140 3 4 600 4 5 210 4 19 310 5 6 230 5 7 200 6 7 320 ...

  9. GameMap(类结构)(不断跟新)

    暂时有个疑问为什么这些需要这么复杂的继承

  10. 5-Highcharts曲线图之轴反转

    <!DOCTYPE> <html lang='en'> <head> <title>5-Highcharts曲线图之轴反转</title> ...