【LeetCode】75. Sort Colors 解題報告
75. Sort Colors / Medium
Given an array nums 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.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library’s sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]
Comstraints
- n == nums.length
- 1 <= n <= 300
- nums[i] is either 0, 1, or 2.
Solution 1: Two Pointers
思路
左右兩個指標,分別代表 0 的右邊界以及 2 的左邊界。
一開始,左指標指向 0,右指標指向 nums.size()-1,第三個 cur 指標一開始指向 0。
如果 nums[cur] 是 0,就把 nums[cur] 與 nums[l] 互換,並將 cur 和 l 向右移動;
如果 nums[cur] 是 2,就把 nums[cur] 與 nums[r] 互換,並將 r 向左移動;
如果 nums[cur] 是 1,就把 cur 向右移動。
核心概念是,將 0 往左丟,2 往右丟,很像是有兩個 pivot 的 quick sort 的某次子排序。
效能
Complexity
- Time Complexity: O(N), which N is the size of nums.
- Space Complexity: O(1)
LeetCode Result
- Runtime: 0 ms
- Memory Usage: 8.2 MB
- https://leetcode.com/submissions/detail/632799396/
Code
1 | class Solution { |
Solution 2: Simply Brute Force
思路
紀錄 0, 1, 2 分別有幾次,並且按照各自的順序與數量寫回 nums 陣列。
效能
Complexity
- Time Complexity: O(N), which N is the size of nums.
- Space Complexity: O(1)
LeetCode Result
- Runtime: 3 ms
- Memory Usage: 8.3 MB
- https://leetcode.com/submissions/detail/632791666/
Code
1 | class Solution { |