1.快慢指针,注意边际条件和慢指针的作用
class Solution {
func removeDuplicates(_ nums: inout [Int]) -> Int {
let n = nums.count
if n == 0 {
return 0
}
var fast = 1, slow = 1
while fast < n {
if nums[fast] != nums[fast - 1] {
nums[slow] = nums[fast]
slow += 1
}
fast += 1
}
return slow
}
}
网友评论