Skip to content

Commit e72ba62

Browse files
committed
feat: Add solution for LeetCode problem 128
1 parent 6cf0af6 commit e72ba62

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// 128. Longest Consecutive Sequence
3+
// https://leetcode.com/problems/longest-consecutive-sequence/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/01.
7+
//
8+
9+
final class Solution {
10+
func longestConsecutive(_ nums: [Int]) -> Int {
11+
let set = Set(nums)
12+
var best = 0
13+
for number in set where !set.contains(number - 1) {
14+
var next = number + 1
15+
while set.contains(next) {
16+
next += 1
17+
}
18+
if best < next - number {
19+
best = next - number
20+
}
21+
}
22+
23+
return best
24+
}
25+
}

0 commit comments

Comments
 (0)