题目
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
- 示例 1:
给定数组 nums = [1,1,2],
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
你不需要考虑数组中超出新长度后面的元素。
- 示例 2:
给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
你不需要考虑数组中超出新长度后面的元素。
- 说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
答案
解法一(笨办法)
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last_scan = None
nums_len = len(nums)
remove_num = 0
for i in range(nums_len):
curr_index = i - remove_num
if nums[curr_index] == last_scan:
nums.remove(nums[curr_index])
remove_num += 1
# 记录当前元素
last_scan = nums[curr_index-1]
else:
# 记录当前元素
last_scan = nums[curr_index]
return len(nums)
解法二
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_len = len(nums)
if nums_len <= 1:
return nums_len
index = 1
for i in range(1, nums_len):
if nums[index - 1] == nums[index]:
nums.pop(index)
else:
index +=1
return index
···