Problems:
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
-Summary-
1. - Move all zeros on the left side of the list.
- Move all twos on the right side of the list.
2. Swap and replace the value in the while loop.
- When we found 0, move to left side
- When we found 1, keep continuing search
- When we found 2, move to right side
이 문제는 dutch national flag algorithm (네덜란드 국기 알고리즘) 으로 아래에 자세한 내용이 있다.
Dutch national flag problem - Wikipedia
The Dutch national flag problem [1] is a computer science programming problem proposed by Edsger Dijkstra.[2] The flag of the Netherlands consists of three colors: red, white and blue. Given balls of these three colors arranged randomly in a line (it does
en.wikipedia.org
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > 2020 LeetCoding Challenge' 카테고리의 다른 글
LeetCode. Largest Divisible Subset (0) | 2020.06.14 |
---|---|
LeetCode. Insert Delete GetRandom O(1) (0) | 2020.06.13 |
LeetCode. Search Insert Position [Binary Search] (0) | 2020.06.11 |
LeetCode. Is Subsequence (0) | 2020.06.10 |
LeetCode. Power of Two (0) | 2020.06.09 |
댓글