LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法。我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征。最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题。具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来。

总体思路是,顺序遍历输入的数组元素,对每个元素先看下HashMap的key中是否已经有这个元素,如果有则无需做任何事情,如果有,再看下这个元素的左邻居也就是比它小1的元素是否在,如果在的话把左邻居的LNodel的next指针指到当前这个元素的LNode,然后再看右邻居的元素是否存在hashmap中,如果在,则把当前指针的next指到右邻居节点上,通过反复这样的操作,最后所有连续的sequece的LNode都被连在一起。

之后再计算哪个连续的链表长度最长。

下面是AC代码:


1 class LNode{
2 int ele;
3 LNode next;
4 public LNode(int _ele){
5 ele = _ele;
6 }
7 }


 1 /**
2 * Longest Consecutive Sequence
3 * Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
4 * O(n)
5 * @param num
6 * @return
7 */
8 public int longestConsecutive(int[] num){
9 //the most important data structure;
10 HashMap<Integer,LNode> kv = new HashMap<Integer,LNode>(num.length);
11 int max = 0;
12 for(int n: num){
13 if(!kv.containsKey(n)){
14 LNode l = new LNode(n);
15 if(kv.containsKey(n-1))
16 kv.get(n-1).next = l;
17 if(kv.containsKey(n+1))
18 l.next = kv.get(n+1);
19 kv.put(n, l);
20 }
21 }
22 Iterator<LNode> it = kv.values().iterator();
23 //k is used to keep tracking those node who has been caculated in other sequences
24 //it is to reduce the time complexity, to insure the O(n) time complexity
25 HashMap<Integer,LNode> k = new HashMap<Integer,LNode>();
26
27 while(it.hasNext())
28 {
29 LNode temp = it.next();
30 if(!k.containsKey(temp.ele))
31 {
32 int nu = 1;
33 while(temp.next!=null){
34 temp = temp.next;
35 k.put(temp.ele, temp);
36 //kv.remove(temp.ele);
37 nu++;
38 }
39 if(nu>max)
40 max = nu;
41 }
42 }
43 return max;
44 }

LeetCode OJ - Longest Consecutive Sequence,布布扣,bubuko.com

时间: 2024-10-03 22:41:49

LeetCode OJ - Longest Consecutive Sequence的相关文章

【LeetCode】Longest Consecutive Sequence

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

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: 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 al

[LeetCode][JavaScript]Longest Consecutive Sequence

Longest Consecutive Sequence 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.

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence 本题难度: Medium/Hard Topic: Data Structure Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Example Given [100, 4, 200, 1,

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions 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 consec

【LeetCode】Longest Consecutive Sequence 解题报告

[题目] 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

LeetCode:Longest Consecutive Sequence

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 i

LeetCode[Map]: Longest Consecutive Sequence

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 i

Java for LeetCode 128 Longest Consecutive Sequence

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 i