最长连续序列

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

说明

要求你的算法复杂度为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. 上传图片的回调函数,callback作为一个函数针对回调函数

    Tool.ImageUpload = function (selector, callback) { /// <summary>图片上传</summary> /// <p ...

  2. EntityFramwork(2Database First) 源地址https://msdn.microsoft.com/zh-cn/data/jj193542

    必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet.     ...

  3. P2763: [JLOI2011]飞行路线

    然而WA了呀,这道分层图,也是不明白为什么WA了=-= ; maxn=; points=; type node=record f,t,l:longint; end; var n,m,k,i,j,u,v ...

  4. Objective-C Foundation框架

    1.字符串 OC由两个字符串:NSString和NSMutableString,NSString代表字符序列不可变的字符串,而NSMutableString则代表字符序列可变的字符串. 1.1 创建字 ...

  5. graphicsMagick 文档

    ImageMagick资料 ---------------------------------------------------------------------------- ImageMagi ...

  6. DebugViewHierarchy

    DebugViewHierarchy(视图调试)是XCode6新出的一项功能,它可以让开发者在程序运行时,动态的查看当前界面的显示情况,包括视图的层次,控件的大小和位置,而且会以3D效果显示当前视图的 ...

  7. cocos2dx 2.0+ 版本,IOS6.0+设置横屏

    使用cocos2dx 自带的xcode模板,是不能正常的设置为横屏的. 一共修改了三个地方: 在项目属性中:Deployment Info中,勾选上 Landscape left,以及 Landsca ...

  8. VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局

    1.通常情况下,我们会根据个人爱好更改VS2013的主题颜色,一开始我喜欢白色,后来我偏爱深色. 依次选择:工具->选项->常规->主题->深色->确定,ok 2.我们在 ...

  9. spring字符编码设置

    <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springf ...

  10. 添加swap分区

    1.创建swap文件,可以单独划分一个分区出来,也可以直接生成一个swap文件 dd if=/dev/zero of=swap bs=1M count=1024 2.格式化为swap文件 mkswap ...