최대 1 분 소요

Question

Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.

주어진 리스트에 있는 0을 맨 오른쪽으로 이동하는 문제이다.

Example

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Input: nums = [0]
Output: [0]

Constraints

  • 1 <= arr.length <= 10^4
  • 1 <= arr[i] <= 10^5

Solution

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        tmp=nums.count(0)
        
        if tmp>0:
            for _ in range(tmp):
                nums.remove(0) # 제일 처음 나오는 0을 제거
                nums.append(0)
  • 직접 푼 풀이이다.


💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기