Published:
Updated:

  • Reference
  • ์‹œ๊ฐ„๋ณต์žก๋„: $O(N)$
  • Runtime 158ms
  • Beats 95.91%


Solution

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