无序数组中最长连续序列
来源:互联网 发布:d80军刀图纸数据 编辑:程序博客网 时间:2024/05/22 05:17
http://stackoverflow.com/questions/7453248/longest-consecutive-sequence-in-an-unsorted-array
1. Question:
You are given an Array of numbers and they are unsorted/random order. You are supposed to find the longest sequence of consecutive numbers in the array.
Note the sequence need not be in sorted order within the array. Here is an example : Input : A[] = {10,21,45,22,7,2,67,19,13,45,12,11,18,16,17,100,201,20,101}
Output is : {16,17,18,19,20,21,22} The solution needs to be of O(n) complexity.
You could have two tables:
Start table: (start-point, length)
End table: (ending-point, length)
3. Step:
When adding a new item, you would check:
1) Does value + 1 exist in start table?
a) If so, delete it and create a new entry of (value, length + 1)
where length is the "current" length.
b) You'd also update the end tablewith the same end point but greater length.
2) Does value - 1 exist in the end table?
a) If so, delete it and create a new entry of (value, length + 1),
b) and this time update the start table (the starting position will be the same,but the length will be increased)
3) If both conditions hold, then you're effectively stitching two existing sequences
together - replace the four existing entries with two new entries, representing the single longer sequence.
4) If neither condition holds, you just create a new entry of length 1 in both tables.
After all the values have been added, you can just iterate over the start table to find the key with the largest value.
1)判断value + 1是否存在于start表中。
a) 如果存在,删除相应的条目,创建一个新条目(value,length + 1),
b) 同时更新end表相应条目,结束数不变,该对应长度加一。
2)判断value - 1是否存在于end表中。
a) 如果存在,删除相应的条目,创建一个新条目(value,length + 1),
b) 同时更新start表相应条目,开始数不表,该对应长度加一。
3)如果在两个表中都存在,则合并两个已经存在的连续序列为一个。将四个条目删除,新建两个条目,每两个条目代表一个连续序列。
4)如果都不存在,则只需要在两个表中创建一个新的长度为1的条目。一直这样等到数组中所有元素处理完毕,然后扫描start表寻找length值最大的那个即可。
{10,21,45,22,7,2,67,19,13,45,12, 11,18,16,17,100,201,20,101}
10
start={(10,1)}
end ={(10,1)}
21
start={(10,1),(21,1)}
end ={(10,1),(21,1)}
45
start={(10,1),(21,1),(45,1)}
end ={(10,1),(21,1),(45,1)}
22
start={(10,1),(21,1),(45,1),(22,1)}
end ={(10,1),(21,1),(45,1),(22,1)}
7
start={(10,1),(21,1),(45,1),(22,1),(7,1)}
end ={(10,1),(21,1),(45,1),(22,1),(7,1)}
2
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1)}
end ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1)}
67
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1)}
end ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1)}
19
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1)}
end ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1)}
13
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
end ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
45
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
end ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
12: 12+1=13
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(12,2)}
end ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,2)}
11:11+1=12;11:11-1=10
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(11,3)}//11+1=12,需要更新起始数
end ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,3)}//11+1=12,结束数仍为13,但是长度加1
start={(10,2),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(11,3)}//11-1=10,起始数据不变,现在有10,11两个数了
end ={(11,2),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,3)}//11-1=0,结束数加1,长度加1
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,1)}//两个条件都满足,说明这个数是一个连接点,可以将start和end表中的数据合并
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,1)}
18:18+1=19
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(18,2)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,2)}
16
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(18,2),(16,1)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,2),(16,1)}
17: 17+1=18,17-1=16
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(17,3),(16,1)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,3),(16,1)}
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(17,3),(16,2)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,3),(17,2)}
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4)}
100
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1)}
201
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1),(201,1)}
end ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1),(201,1)}
20:20+1=21;20-1=19;
start={(10,4),(20,3),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1),(201,1)}
end ={(13,4),(22,3),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1),(201,1)}
start={(10,4),(20,3),(45,1),(45,1),(7,1),(2,1),(67,1),(16,5),(100,1),(201,1)}
end ={(13,4),(22,3),(45,1),(45,1),(7,1),(2,1),(67,1),(20,5),(100,1),(201,1)}
start={(10,4),(20,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,1),(201,1)}
end ={(13,4),(22,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,1),(201,1)}
101: 101-1=100;
start={(10,4),(20,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,2),(201,1)}
end ={(13,4),(22,7),(45,1),(45,1),(7,1),(2,1),(67,1),(101,2),(201,1)}
- 无序数组中最长连续序列
- 无序数组中找到最长连续子序列
- O(n)时间找出无序数组中最长的连续递增序列
- 无序数组中最长非降子序列长度
- 数组中最长连续序列
- 一个无序整数数组中找到最长连续序列(Longest Consecutive Sequence)和两个元素使得相差最小
- 在无序序列中找出最长的连续序列 Longest Consecutive Sequence
- 无序整形数组,如何找到最长连续序列长度,时间复杂度O(n)
- 无序数组的最长递增子序列
- 数组——数组中最长连续序列
- 数组中的最长连续序列
- Longest Consecutive Sequence 从无序数组中找最长的连续数字 @LeetCode
- 找出无序数组的最长连续子数组
- 求数组中最长的连续子序列
- 获取一个数组中最长的连续的元素序列
- longest-consecutive-sequence,数组中最长的连续序列
- 数组中最长的连续序列(longest consecutive sequence)
- 求数组中最长连续递增子序列
- 如何判断用户用的是Android手机还是IOS手机
- Linux操作系统CPU信息
- unity3d,通过WWW加载图片纹理
- hql中setDate和setTimeStamp的区别
- Quartz入门实例7-处理被中断的Job
- 无序数组中最长连续序列
- POJ 1860 SPFA
- HDU-4901 The Romantic Hero DP
- 浪潮记(1):初入浪潮
- Zookeeper master和server
- 快速的批量修改重命名.net程序的命名空间
- 阿里云服务器安装配置Apache+php服务器
- 双目相机--双目视差与深度距离关系推导详解
- 事务的隔离性