Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lcof2/剑指 Offer II 119. 最长连续序列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,33 @@ var longestConsecutive = function (nums) {
};
```

#### Swift

```swift
class Solution {
func longestConsecutive(_ nums: [Int]) -> Int {
let numSet: Set<Int> = Set(nums)
var longestStreak = 0

for num in nums {
if !numSet.contains(num - 1) {
var currentNum = num
var currentStreak = 1

while numSet.contains(currentNum + 1) {
currentNum += 1
currentStreak += 1
}

longestStreak = max(longestStreak, currentStreak)
}
}

return longestStreak
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
22 changes: 22 additions & 0 deletions lcof2/剑指 Offer II 119. 最长连续序列/Solution2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
func longestConsecutive(_ nums: [Int]) -> Int {
let numSet: Set<Int> = Set(nums)
var longestStreak = 0

for num in nums {
if !numSet.contains(num - 1) {
var currentNum = num
var currentStreak = 1

while numSet.contains(currentNum + 1) {
currentNum += 1
currentStreak += 1
}

longestStreak = max(longestStreak, currentStreak)
}
}

return longestStreak
}
}
Loading