remove_deulicates

  • 2022-12-14
  • 浏览 (496)

remove_deulicates.py 源码

# 删除排序数组中的重复项
from typing import List


class Solution:
    # 新开一个数组来存储
    def removeDuplicates_1(self, nums: List[int]) -> int:
        if len(nums) <= 1:
            return len(nums)
        temp = [nums[0]]
        for n in nums:
            if n != temp[-1]:
                temp.append(n)
        nums[:] = temp[:]
        return len(temp)

    # 双指针
    def removeDuplicates_2(self, nums: List[int]) -> int:
        i, j = 0, 1
        while j < len(nums):
            if nums[i] == nums[j]:
                j += 1
            else:
                nums[i + 1] = nums[j]
                i, j = i + 1, j + 1
        return i + 1

你可能感兴趣的文章

best_time_buy_sell_stock

best_time_buy_sell_stock_ii

climb_statirs

0  赞