lintcode: 最长连续序列
最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度。
说明
要求你的算法复杂度为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: 最长连续序列的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
- [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [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 ...
- [leetcode-128] 最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...
随机推荐
- 一些Iphone sqlite 的包装类
相信很多人用iphone的Sqlite不会直接用C的方法,要么自己包装一层Object c的访问方法,要么用CoreData,下面我整理些目前所了结的一些Sqlite 包装类. 1.CoreData ...
- Android实现Button事件的处理
Android实现Button事件的处理 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是最基本的线性布局,给每个控件设立id值,以供代 ...
- (转)Unity3D游戏开发 NGUI之渐变加载到100%的Loading场景进度条
NGUI 现有的进度条存在的问题: 进度条跳跃式前进,加载到90%后卡住,突然进入下一个场景.接下来就是解决这个问题. 背景 通常游戏的主场景包含的资源较多,这会导致加载场景的时间较长.为了避免这个问 ...
- 多线程 -- NSThread
NSThread NSThread 一个NSThread对象就代表一条线程 创建线程的几种方式 alloc/init // 1.创建线程 NJThread *thread = [[NJThread a ...
- C6011 正在取消对 null 指针的引用
- SqlServer中Sql语句的逻辑执行顺序
准备数据 Sql脚本如下,两张表,一张客户表Customers只包含customerid和city字段,一张订单表Orders包含orderid和customerid(关联Customers的cust ...
- kibana 修改Ico图标
修改此路径下的E:\happy\kinbana\kibana-4.2.2-windows\kibana-4.2.2-windows\optimize\bundles的commons.bundle.js ...
- android开发,静音录制视频,在一般清晰度的前提下保证文件大小越小越好
public void startRecord() { mediarecorder = new MediaRecorder();// 创建mediarecorder对象 mCamera = getCa ...
- python 数据结构-集合set
原文地址:http://docs.pythontab.com/python/python3.4/datastructures.html#tut-tuples 集合是一个无序不重复元素的集. 基本功能包 ...
- SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...