Skip to content

Commit 45916f8

Browse files
committed
128_Longest_Consecutive_Sequence
1 parent b86b974 commit 45916f8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ I have solved quite a number of problems from several topics. See the below tabl
198198
|86| **[914. X of a Kind in a Deck of Cards](https://tinyurl.com/y39y2k32)** | [Python](https://tinyurl.com/wu6rdaw/914_X_of_a_Kind_in_a_Deck_of_Cards.py), [Swift](https://tinyurl.com/wuja3c4/914_X_of_a_Kind_in_a_Deck_of_Cards.swift) | --- | Easy (Not So) | |
199199
|87| **[1275. Find Winner on a Tic Tac Toe Game](https://tinyurl.com/y323arde)** | [Python](https://tinyurl.com/wu6rdaw/1275_Find_Winner_on_a_Tic_Tac_Toe_Game.py), [Swift](https://tinyurl.com/wuja3c4/1275_Find_Winner_on_a_Tic_Tac_Toe_Game.swift) | [Art 1](https://tinyurl.com/yyc54qj5) | Easy (Not So) | |
200200
|88| **[532. K-diff Pairs in an Array](https://tinyurl.com/yyrhmynd)** | [Python](https://tinyurl.com/wu6rdaw/532_K-diff_Pairs_in_an_Array.py), [Swift](https://tinyurl.com/wuja3c4/532_K-diff_Pairs_in_an_Array.swift) | [Art 1](https://tinyurl.com/y5tg5aqd) | Medium | |
201+
|89| **[128. Longest Consecutive Sequence](https://tinyurl.com/yxzab8pw)** | [Python](https://tinyurl.com/wu6rdaw/128_Longest_Consecutive_Sequence.py), [Swift](https://tinyurl.com/wuja3c4/128_Longest_Consecutive_Sequence.swift) | [Art 1](https://tinyurl.com/y5e34662) | Hard | |
201202

202203

203204
</p>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Foundation
2+
class Solution {
3+
func longestConsecutive(_ nums: [Int]) -> Int {
4+
let numsSet = Set<Int>(nums)
5+
var maxSequence = 0
6+
for num in numsSet {
7+
if !numsSet.contains(num - 1) {
8+
var end = num
9+
while numsSet.contains(end + 1) {
10+
end += 1
11+
}
12+
maxSequence = max(maxSequence, end - num + 1)
13+
}
14+
}
15+
return maxSequence
16+
}
17+
}

0 commit comments

Comments
 (0)