【python】Leetcode每日一题-删除有序数组中的重复项2
【python】Leetcode每日一题-删除有序数组中的重复项2
【题目描述】
给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}
示例1:
输入:nums = [1,1,1,2,2,3]
输出:5, nums = [1,1,2,2,3]
解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
示例2:
输入:nums = [0,0,1,1,1,1,2,3,3]
输出:7, nums = [0,0,1,1,2,3,3]
解释:函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
提示:
1 <= nums.length <= 3 * 10^4
-10^4 <= nums[i] <= 10^4
nums 已按升序排列
【分析】
-
思路(双指针
容易想到思路,设置一个当前元素值变量,一个重复数量变量,一个index记录当前指针位置。
-
AC代码
class Solution: def removeDuplicates(self, nums: List[int]) -> int: if nums == []: return 0 index = 0 now = nums[0] num = 0 for i in range(len(nums)): if nums[index] == now and num == 2: nums.pop(index) elif nums[index] == now: num += 1 index += 1 else: num = 1 now = nums[index] index += 1 return index
优化:直接覆盖。
class Solution: def removeDuplicates(self, nums: List[int]) -> int: if nums == []: return 0 index = 0 for i in range(len(nums)): if index < 2: index += 1 continue if nums[i] == nums[index-2]: continue else: nums[index] = nums[i] index += 1 return index