Skip to content

Commit 2327613

Browse files
author
liwentian
committed
longest consecutive sequence
1 parent 61c75c0 commit 2327613

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎Facebook.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Facebook题目列表(24)
4545
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/GroupAnagrams.java)|
4646
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/SimplifyPath.java)|
4747
|79|[Word Search](https://leetcode.com/problems/word-search/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WordSearch.java)|
48-
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RandomPickIndex.java)|
48+
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RandomPickIndex.java)|
49+
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java)|

‎Leetcodes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BestTimeBuySellStock.java)|100|
3535
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ValidPalindrome.java)|100|
3636
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WordLadder.java)|70|
37+
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java)|60|
3738
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/CopyListWithRandomPointer.java)|95|
3839
|139|[Word Break](https://leetcode.com/problems/word-break/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WordBreak.java)|80|
3940
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LinkedListCycle.java)|75|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.inuker.solution;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Created by dingjikerbo on 16/11/22.
7+
*/
8+
9+
public class LongestConsecutiveSequence {
10+
11+
public int longestConsecutive(int[] nums) {
12+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
13+
14+
int res = 0;
15+
16+
for (int i = 0; i < nums.length; i++) {
17+
int n = nums[i];
18+
19+
if (!map.containsKey(n)) {
20+
int left = map.containsKey(n - 1) ? map.get(n - 1) : 0;
21+
int right = map.containsKey(n + 1) ? map.get(n + 1) : 0;
22+
int len = left + right + 1;
23+
24+
// 这句一定不能掉,因为map会查重的,如果这里n没丢到map里,后面再出现重复的n会被覆盖
25+
map.put(n, len);
26+
27+
res = Math.max(res, len);
28+
29+
map.put(n - left, len);
30+
map.put(n + right, len);
31+
}
32+
}
33+
34+
return res;
35+
}
36+
}

0 commit comments

Comments
 (0)