2460. Apply Operations to an Array26. Remove Duplicates from Sorted Array 是類似題。


class Solution:
    def applyOperations(self, nums: List[int]) -> List[int]:
        n = len(nums)

        for i in range(n - 1):
            if nums[i] == nums[i + 1]:
                nums[i] *= 2
                nums[i + 1] = 0

        index = 0
        for i in range(n):
            if nums[i] != 0:
                nums[index] = nums[i]
                index += 1

        while index < n:
            nums[index] = 0
            index += 1

        return nums

時間空間複雜度

時間複雜度: O(N)

算遍三次 nums ,還是 O(N) 複雜度

空間複雜度:O(1)

都沒有開闢額外空間