leetcode-array, since 2022-05-31

(2022.05.31 Tues)

    1. Remove Duplicates from Sorted Array (easy)
      array非递减,有重复,返回这个array中独一无二的元素个数,并放在array前面几位。提示:双指针法
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        left = 0
        for right,e in enumerate(nums):
            if nums[left] != e:
                left += 1
                nums[left] = e
        return left+1

类似提醒还有27 remove duplicates

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        left = 0
        for e in nums:
            if e != val:
                nums[left] = e
                left += 1
            else:
                continue
        return left

你可能感兴趣的:(leetcode-array, since 2022-05-31)