728x90
arr 내에 0이 있으면, 0을 하나씩 리스트에 더 넣되 리스트의 길이는 동일하게 가져가는 문제
class Solution(object):
def duplicateZeros(self, arr):
"""
:type arr: List[int]
:rtype: None Do not return anything, modify arr in-place instead.
"""
n = len(arr)
for i in reversed(range(n)):
if arr[i] == 0:
arr.insert(i,0)
del arr[n:]
- arr의 길이를 n으로 잡고
- for 문을 역순으로 진행, i번째 항목이 0이라면 i 자리에 0을 insert함
- n보다 더 길어진 arr의 리스트 뒷부분을 제거
Runtime : 0ms
Memory : 12.99MB(13.64%)
Runtime은 빠르지만 memory가 많이 들어가는 방법인듯 하다.
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python] 27. Remove Element (0) | 2025.01.18 |
---|---|
[Python] 88. Merge Sorted Array (0) | 2025.01.17 |
[Python] 1769. Minimum Number of Operations to Move All Balls to Each Box (0) | 2025.01.15 |
[Python] 2657. Find the Prefix Common Array of Two Arrays (0) | 2025.01.14 |
[Python] 3223. Minimum Length of String After Operations (0) | 2025.01.14 |