Skip to content

Commit 6ca6bfe

Browse files
committed
https://leetcode.cn/problems/longest-consecutive-sequence/
1 parent 505e27b commit 6ca6bfe

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/longest-consecutive-sequence/
49+
4850
https://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/
4951

5052
https://leetcode.cn/problems/smallest-subarrays-with-maximum-bitwise-or/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default function longestConsecutive(nums: number[]): number {
2+
const num_set: Set<number> = new Set();
3+
for (const num of nums) {
4+
num_set.add(num);
5+
}
6+
7+
let longestStreak = 0;
8+
9+
for (const num of num_set) {
10+
if (!num_set.has(num - 1)) {
11+
let currentNum = num;
12+
let currentStreak = 1;
13+
14+
while (num_set.has(currentNum + 1)) {
15+
currentNum += 1;
16+
currentStreak += 1;
17+
}
18+
19+
longestStreak = Math.max(longestStreak, currentStreak);
20+
}
21+
}
22+
23+
return longestStreak;
24+
}

0 commit comments

Comments
 (0)