This commit is contained in:
2026-07-23 16:56:50 +08:00
parent 124ad9afdf
commit 92784ffb0f
36 changed files with 2015 additions and 608 deletions
+55
View File
@@ -0,0 +1,55 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/sort-colors/?envType=study-plan-v2&envId=top-100-liked
// 75. 颜色分类
//
// 给定一个包含红色、白色和蓝色、共 `n` 个元素的数组 `nums` ,原地 对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
// 我们使用整数 `0`、 `1` 和 `2` 分别表示红色、白色和蓝色。
// 必须在不使用库内置的 sort 函数的情况下解决这个问题。
// 示例 1
// 输入:nums = [2,0,2,1,1,0]
// 输出:[0,0,1,1,2,2]
// 示例 2
// 输入:nums = [2,0,1]
// 输出:[0,1,2]
//
// 提示:
// - `n == nums.length`
// - `1 <= n <= 300`
// - `nums[i]` 为 `0`、`1` 或 `2`
// 进阶:
// - 你能想出一个仅使用常数空间的一趟扫描算法吗?
//
func sortColors(nums []int) {
}
func TestSortColors(t *testing.T) {
cases := []struct {
name string
nums []int
want []int
}{
{"example1", []int{2, 0, 2, 1, 1, 0}, []int{0, 0, 1, 1, 2, 2}},
{"example2", []int{2, 0, 1}, []int{0, 1, 2}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
sortColors(c.nums)
if len(c.nums) != len(c.want) {
t.Errorf("got %v, want %v", c.nums, c.want)
return
}
for i := range c.nums {
if c.nums[i] != c.want[i] {
t.Errorf("got %v, want %v", c.nums, c.want)
return
}
}
})
}
}