leetcode26. 删除排序数组中的重复项 python实现

2020-02-28  本文已影响0人  vvblack4

题目:

leetcode26题目描述

解法1:

    def removeDuplicates(self, nums: List[int]) -> int:
        if not nums:
            return 0
        
        temp = nums[0]
        count = 0
        for i in range(1,len(nums)):
            if temp == nums[i-count]:
                del nums[i-count]
                count += 1
            else:
                temp = nums[i-count]
        return len(nums)

解法2:

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        if not nums:
            return 0
        i=0
        for j in range(1,len(nums)):
            if nums[i]!=nums[j]:
                i += 1
                nums[i]=nums[j]
        return i+1
上一篇 下一篇

猜你喜欢

热点阅读