您的位置 首页 golang

用Golang刷Leetcode 26. Remove Duplicates from Sorted Array

思路

两个指针差一然后一起前进,发现相同的就执行删除动作,比较简单

code

func removeDuplicates(nums []int) int {
for i, j := 0, 1; i < j && j < len(nums); {
if nums[j] == nums[i] {
nums =  append (nums[:i], nums[i+1:]...)
j--
i--
}
j++
i++
}
return len(nums)
}
 

更多内容请移步我的repo:

文章来源:智云一二三科技

文章标题:用Golang刷Leetcode 26. Remove Duplicates from Sorted Array

文章地址:https://www.zhihuclub.com/90138.shtml

关于作者: 智云科技

热门文章

网站地图