Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
思路
因为已经有序了,挨个往下判断就行,注意边界条件不要超过数组的范围即可
code
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
cur = 0
while cur < len(nums) - 1:
if nums[cur] == nums[cur + 1]:
nums.remove(nums[cur])
cur -= 1
cur += 1
return len(nums)