Skip to content

Commit abc6485

Browse files
committed
longest-consecutive-sequence solved
1 parent 999f6e3 commit abc6485

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func longestConsecutive(_ nums: [Int]) -> Int {
3+
let numSet = Set(nums)
4+
var longest = 0
5+
6+
for num in numSet {
7+
// ์—ฐ์† ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ์ธ์ง€ ํ™•์ธ
8+
if !numSet.contains(num - 1) {
9+
var currentNum = num
10+
var currentStreak = 1
11+
12+
while numSet.contains(currentNum + 1) {
13+
currentNum += 1
14+
currentStreak += 1
15+
}
16+
17+
longest = max(longest, currentStreak)
18+
}
19+
}
20+
21+
return longest
22+
}
23+
}

0 commit comments

Comments
ย (0)