LeetCode 1089 Duplicate Zeros (Python)

Posted by 小明MaxMing on February 21, 2022

题目

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.

解题思路

遍历两次,第一次找到哪个数会放在数组的最后一位,如果最后是0可能写在最后一位或者两位,之后再向前遍历一次,把剩下的数填到数组中

代码

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        n, ct, cur, f = len(arr), 0, 0, False
        for num in arr:
            ct += 2 if num == 0 else 1
            if ct >= n:
                f = ct == n + 1
                break
            cur += 1

        right = n - 1
        if f:
            arr[-1] = 0
            cur -= 1
            right -= 1
        while cur >= 0:
            arr[right] = arr[cur]
            if arr[cur] == 0:
                arr[right - 1] = 0
                right -= 1    
            right -= 1
            cur -= 1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅