一、原题目
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice 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.
二、题目大意
这个题目与前两个版本的基本要求差不多,变化的是列表nums中相同元素个数不能超过两个。
(如有不理解,请看本人对于前两个版本的解释。)
三、思路分析
本人的思路:
利用“设置独立下标”避免下标越界的方式,可以解决这个问题。
(如有不理解,请看本人对于前两个版本的解释。)
四、具体代码
本人的代码:
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n=1
while n<len(nums):
while nums.count(nums[n])>2:
nums.remove(nums[n])
else:
n+=1
return len(nums)
五、两者差别
主要是多了一个while判断和count计数
六、知识点总结
本人做这道题目很快,有了前两道基础,这里没有知识点。如有不理解,请看本人对于前两个版本的解释。)
七、.来源
题目连接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
作者原文解答:无
座右铭:站在别人的思想上,看见自己的不足,传播错误的经验,愿君不重蹈覆辙。
由于受限于本人经验,难免不足,如有建议,欢迎留言交流。
说明:以上代码都能通过测试,本人亲测。如果喜欢,请点赞,您的鼓励是本人前进的最好动力。
---------------------
作者:路漫漫,远修兮
来源:CSDN
原文:https://blog.csdn.net/qq_41827968/article/details/88756403
版权声明:本文为博主原创文章,转载请附上博文链接!