File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
48
+ https://leetcode.cn/problems/longest-consecutive-sequence/
49
+
48
50
https://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/
49
51
50
52
https://leetcode.cn/problems/smallest-subarrays-with-maximum-bitwise-or/
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments