[LeetCode][Python3] 283. Move Zeroes
2018. 10. 5. 02:09 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/move-zeroes/description/
My Solution :
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count = 0
for i in range(len(nums)):
if nums[i] == 0:
count += 1
elif count > 0:
nums[i], nums[i-count] = nums[i-count], nums[i]
Comment :
0이 아닌 값을 만나면 0과 swap하는 전략인데, swap할 0의 index는 0이 아닌 값의 index에서 0의 갯수만큼을 뺀 index가 된다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 572. Subtree of Another Tree (0) | 2018.10.07 |
---|---|
[LeetCode][Python3] 538. Convert BST to Greater Tree (0) | 2018.10.07 |
[LeetCode][Python3] 438. Find All Anagrams in a String (0) | 2018.10.07 |
[LeetCode][Python3] 437. Path Sum III (0) | 2018.10.07 |
[LeetCode][Python3] 234. Palindrome Linked List (0) | 2018.10.04 |
[LeetCode][Python3] 226. Invert Binary Tree (0) | 2018.10.03 |
[LeetCode][Python3] 206. Reverse Linked List (0) | 2018.10.01 |
[LeetCode][Python3] 198. House Robber (0) | 2018.10.01 |
최근에 달린 댓글 최근에 달린 댓글