관리 메뉴

솜씨좋은장씨

[leetCode] 283. Move Zeroes (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 283. Move Zeroes (Python)

솜씨좋은장씨 2020. 6. 19. 21:18
728x90
반응형

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

 

Example:

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

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

Solution

class Solution:
    def moveZeroes(self, nums):
        for i in range(len(nums))[::-1]:
            if nums[i] == 0:
                nums.pop(i)
                nums.append(0)

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments