관리 메뉴

솜씨좋은장씨

[leetCode] 1089. Duplicate Zeros (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1089. Duplicate Zeros (Python)

솜씨좋은장씨 2020. 10. 7. 16:58
728x90
반응형

Given a fixed length array arr of integers, 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, do not return anything from your function.

 

Example 1:

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3]

Note:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

Solution

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        answer_temp = []
        
        for num in arr:
            if num == 0:
                answer_temp.append(num)
            answer_temp.append(num)
            
        answer = answer_temp[:len(arr)]
        
        for i in range(len(arr)):
            arr[i] = answer[i]

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments