颜色分类

颜色分类

CategoryDifficultyLikesDislikes
algorithmsMedium (60.43%)1611-
Tags

array | two-pointers | sort

Companies

facebook | microsoft | pocketgems

给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums ,**原地**对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

我们使用整数 012 分别表示红色、白色和蓝色。

必须在不使用库内置的 sort 函数的情况下解决这个问题。

示例 1:

1
2
输入:nums = [2,0,2,1,1,0]
输出:[0,0,1,1,2,2]

示例 2:

1
2
输入:nums = [2,0,1]
输出:[0,1,2]

提示:

  • n == nums.length
  • 1 <= n <= 300
  • nums[i]012

进阶:

  • 你能想出一个仅使用常数空间的一趟扫描算法吗?

Discussion | Solution

解法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// @lc code=start
impl Solution {
    /// ## 解题思路
    /// - 双指针
    /// [ 0, 0, 1, 0, 2, 0, 1, 0, 2, 1 ]
    /// [ 0, 0, 0, 0, 0, 1, 1, 1, 2, 2 ]
    ///                off_1    off_2
    pub fn sort_colors(nums: &mut Vec<i32>) {
        let (mut i, mut off_1, mut off_2) = (0, 0, nums.len());
        while i < off_2 {
            match nums[i] {
                0 => {
                    if i != off_1 || nums[off_1] != 0 {
                        nums.swap(i, off_1); // 将当前的0放置到0区间后面
                    }
                    off_1 += 1; // 0区间元素增多, 1区间左边界右移
                    i += 1; //
                }
                2 => {
                    off_2 -= 1; // 移动2区间左边界
                    nums.swap(i, off_2); // 将当前的2移动到2区间左边
                                         // nums[off_2]交换前的数没有访问过, 所以i不递增
                }
                _ => {
                    i += 1; // 当前元素为1,
                }
            }
        }
    }
}
// @lc code=end

struct Solution;
updatedupdated2024-08-252024-08-25