每日一道算法题之LeetCode26
LeetCode26 删除排序数组中的重复项
# 删除重复元素,解题方法:双指针
# 1 定义2个指针,慢指针i,从0开始;快指针j,从1开始
# 2 移动指针,如果当前nums[i] = nums[j],则j加1,i不动;如果不相等,则i,j均加1
1 class Solution: 2 def removeDuplicates(self, nums: List[int]) -> int: 3 n = len(nums) 4 i, j = 0, 1 5 while(j<n): 6 if nums[i] == nums[j]: 7 j += 1 8 else: 9 nums[i+1] = nums[j] 10 i += 1 11 j += 1 12 return len(nums[0:i+1])#返回数组的0到i个元素,索引为0到i+1.