...
Code Block | ||
---|---|---|
| ||
func removeDuplicates(nums []int) int { i := 0 j := 1 for ; j < len(nums); j++ { if nums[j] == nums[j-1] { // duplicate continue } nums[i+1] = nums[j] i++ } return i+1 } |
另一种解法:思路类似,但是代码组织形式不同
Code Block | ||
---|---|---|
| ||
func removeDuplicates(nums []int) int {
slow, fast := 0, 0
for fast < len(nums) {
if nums[fast] != nums[slow] {
slow++
nums[slow] = nums[fast]
}
fast++
}
return slow+1
} |