Clear2 / Algorithm

算法训练
MIT License
0 stars 0 forks source link

删除排序数组中的重复项 #9

Open Clear2 opened 4 years ago

Clear2 commented 4 years ago

// 原地修改数组
func removeDuplicates(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    var i = 0;
    for j := 1; j < len(nums); j++ {
        if (nums[j] != nums[i]) {
            i++
            nums[i] = nums[j]
        }
    }
    return i + 1
}
Clear2 commented 4 years ago
var num1 = []int{0,0,1,1,1,2,2,3,3,4}
length := removeDuplicates(num1)