We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6cf0af6 commit e72ba62Copy full SHA for e72ba62
longest-consecutive-sequence/WhiteHyun.swift
@@ -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