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.
Input : nums = [0,1,0,3,12]
Output : [1,3,12,0,0]
Input : nums = [0]
Output : [0]
1<= nums.length <= 10^4
-2^31 <= nums[i] <= 2^31 - 1
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
non_zero = 0 # 0의 개수
index = 0 # 0이 아닌 숫자를 넣어줄 index
for num in nums :
if num != 0 :
nums[index] = num
index += 1
non_zero += 1
nums[non_zero:] = [0] * (len(nums)-non_zero)