Skip to content

Commit dbd9bff

Browse files
committed
leet 128 longest consecutive sequences [medium]
1 parent 0a3f193 commit dbd9bff

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

허현빈/5주차/260126.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
const set = new Set(nums);
7+
8+
let max = 0;
9+
for (let num of set) {
10+
let count = 1;
11+
if (!set.has(num - 1)) {
12+
let idx = 1;
13+
while (1) {
14+
if (set.has(num + idx)) {
15+
idx++;
16+
count++;
17+
} else {
18+
break;
19+
}
20+
}
21+
max = Math.max(max, count);
22+
}
23+
}
24+
return max;
25+
};

0 commit comments

Comments
 (0)