Leetcode-26.从排序数组中删除重复项
Leetcode-26.从排序数组中删除重复项
中文:
给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度。
不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点。
示例:
给定数组: nums = [1,1,2],
你的函数应该返回新长度 2, 并且原数组 nums 的前两个元素必须是 1 和 2
不需要理会新的数组长度后面的元素
英文:
Remove Duplicates from Sorted Array
Given a sorted array, 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.
Example:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.
以下是我的解答。
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
rtype = ''
while i < len(nums):
if rtype != nums[i]:
rtype = nums[i]
i = i + 1
else:
nums.pop(i)
return(len(nums))
标题:Leetcode-26.从排序数组中删除重复项
作者:xflcx1991
地址:https://www.xffish.info/articles/2018/03/11/1520730000000.html