//GitHub https://github.com/vellimole0621
LeetCode - Move Zeroes
문제 설명
:
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.
=> 숫자로 이루어진 배열이 주어져있다. 배열의 요소 중 0이 모두 오른쪽으로 정렬되도록 하라. 단, 다른 배열로 복사 하지 말고. in-place 상태로 해결하라.
내 풀이
:
반복문 생성, 요소가 0일 때 => 내부에 반복문 생성해서, 0이 아닌 값을 찾아. 발견했을시 0이 아닌 값과 위치를 바꿔준다.
# Move Zeroes
# https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3157/
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(0,len(nums)):
if nums[i] == 0:
for j in range(i+1, len(nums)):
if nums[j] != 0:
nums[i] = nums[j]
nums[j] = 0
break
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Remove Duplicates from Sorted Array (0) | 2023.09.18 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Replace Elements with Greatest Element on Right Side (0) | 2023.09.17 |
[파이썬 Python/알고리즘] LeetCode - Check If N and Its Double Exist (0) | 2023.09.11 |
[파이썬 Python/알고리즘] LeetCode - Remove Element (0) | 2023.09.07 |
[파이썬 Python/알고리즘] LeetCode - Merge Sorted Array (0) | 2023.09.05 |