赞
踩
给定的数组为有序的,意味着重复的元素是相邻的。将非重复项都移到数组的左边,即可完成题目要求。
class Solution: def removeDuplicates(self, nums: List[int]) -> int: n = len(nums) if n <= 1: return n current = 1 front = 0 while current < n: #双指针,一快一慢 if nums[current] != nums[front]: nums[front + 1] = nums[current] front += 1 current += 1 else: current += 1 return front + 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。