func longestConsecutive(_ nums: [Int]) -> Int {
let len = nums.count
if len == 0 {
return 0
}
var res = 1
var temp = 1
let sortNums = nums.sorted()
for i in 1..<len {
let num2 = sortNums[i] ,num1 = sortNums[i - 1]
if num2 - num1 == 1 {
temp += 1
res = max(temp, res)
}else if num2 == num1{
continue
}
else{
temp = 1
}
}
return res
}
网友评论