class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# We will use the integers 0, 1, and 2
rgb = [0, 0, 0]
for n in nums:
rgb[n] += 1
i = 0
for n in range(len(rgb)):
for _ in range(rgb[n]):
nums[i] = n
i += 1
https://youtu.be/4xbWSRZHqac?si=eIOF8amP3_ulRLQu
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
l, r = 0, len(nums)-1
i = 0
def swap(i, j):
tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp
while i<=r:
if nums[i] == 0:
swap(l, i)
l+=1
elif nums[i] == 2:
swap(r, i)
r -= 1
i -= 1
i += 1
추가 문제
280. Wiggle Sort: https://leetcode.com/problems/wiggle-sort/description/
324. Wiggle Sort II: https://leetcode.com/problems/wiggle-sort-ii/description/
'LeetCode > 주제별 보충' 카테고리의 다른 글
LinkedList (Queue): 225. Implement Stack using Queues (0) | 2025.02.01 |
---|---|
LinkedList (Queue): 1700. Number of Students Unable to Eat Lunch ★ (0) | 2025.02.01 |
Graphs (Union Find): 684. Redundant Connection (1) | 2025.01.28 |
Graphs(싸이클확인, Union Find): 323. Number of Connected Components in an Undirected Graph ★★★ (0) | 2025.01.28 |
List(싸이클탐지): 142. Linked List Cycle II (0) | 2025.01.27 |