Published:
Updated:

  • Reference
  • μ‹œκ°„λ³΅μž‘λ„: O(N)
  • Runtime 158ms
  • Beats 95.91%


SolutionPermalink

class Solution:
    # 0인 것과, 0이 μ•„λ‹Œ 것 λ‚˜λˆ μ„œ 생각
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        array_zero = []
        array = []

        for num in nums:
            # 0인 λ°°μ—΄ μž¬μƒμ„±
            if num == 0:
                array_zero.append(num)
            # 0이 μ•„λ‹Œ 일반 λ°°μ—΄ μž¬μƒμ„±
            else:
                array.append(num)

            # μ•„λž˜μ²˜λŸΌ μ“Έ μˆ˜λ„ 있음 (3ν•­)
            # array_zero.append(num) if num == 0 else array.append(num)
        
        # return ν•˜λŠ” 게 μ•„λ‹ˆλΌ nums에 λ‹€μ‹œ λ„£μ–΄μ€˜μ•Ό 함
        # λ°°μ—΄[:]은 전체 슬라이슀λ₯Ό 의미 -> λ°°μ—΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό μ„ νƒν•˜λŠ” 것과 κ°™μŒ
        # μ΄λ ‡κ²Œ ν•˜λ©΄ κ°€λ¦¬ν‚€λŠ” 객체가 λ³€κ²½λ˜μ§€ μ•Šκ³  ν•΄λ‹Ή 객체의 λ‚΄μš©λ§Œ 변경됩 -> 제자리 μˆ˜μ •
        nums[:] = array + array_zero

Leave a comment