26 Remove Duplicates from Sorted Array
Posted fan-2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26 Remove Duplicates from Sorted Array相关的知识,希望对你有一定的参考价值。
def removeDuplicates(nums): leng = len(nums) k = 0 #if leng ==0: #return False for x in range(1,leng) : print(k,x) print(nums) if nums[k] != nums[x]: k=k+1 nums[k] = nums[x] print(k+1) nums = [0,0,1,1,1,2,2,3,3,4] removeDuplicates(nums)
非常好玩的双指针的题目,一开始的想法是转set然后直接len()调用一下解决,
时间复杂度度为O(1),且提供的是有序数列
k为慢指针,负责统计个数
x为快指针,负责遍历数组
在两者不同的时候,k前进一格,进入下一阶段,保证前k个是有序且不重复的
以上是关于26 Remove Duplicates from Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章
26. Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted Array
#26 Remove Duplicates from Sorted Array
leetcode 26. Remove Duplicates from Sorted Array 80. Remove Duplicates from Sorted Array II