https://leetcode-cn.com/problems/first-missing-positive/submissions/
func firstMissingPositive(_ nums: [Int]) -> Int {
var NumArr = nums
for idx in stride(from: 0, to: NumArr.count, by: 1) {
while ((NumArr[idx] >= 1 && NumArr[idx] <= NumArr.count) && (NumArr[idx] != NumArr[NumArr[idx] - 1])) {
let tmp = NumArr[idx]
NumArr[idx] = NumArr[tmp - 1]
NumArr[tmp - 1] = tmp
}
}
var result = NumArr.count + 1
for (idx, item) in NumArr.enumerated() {
if item != idx + 1 {
result = idx + 1
break
}
}
return result
}
网友评论