Skip to content

Commit 5e56bdd

Browse files
aQuaaQua
aQua
authored and
aQua
committed
75 added
1 parent d6f742a commit 5e56bdd

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

Algorithms/0075.sort-colors/README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
## 题目
44
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
55

6-
7-
86
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
97

10-
11-
12-
Note:
13-
You are not suppose to use the library's sort function for this problem.
14-
8+
**Note:**You are not suppose to use the library's sort function for this problem.
159

1610
click to show follow up.
17-
18-
11+
```
1912
Follow up:
2013
A rather straight forward solution is a two-pass algorithm using counting sort.
2114
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
2215
Could you come up with an one-pass algorithm using only constant space?
23-
16+
```
2417
## 解题思路
2518

2619
见程序注释

Algorithms/0075.sort-colors/sort-colors_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0075
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -19,7 +19,7 @@ type para struct {
1919

2020
// ans 是答案
2121
type ans struct {
22-
one
22+
one []int
2323
}
2424

2525
func Test_Problem0075(t *testing.T) {
@@ -29,20 +29,20 @@ func Test_Problem0075(t *testing.T) {
2929

3030
question{
3131
para{
32-
,
32+
[]int{0, 1, 2, 0, 1, 2, 0, 1, 2},
3333
},
3434
ans{
35-
,
35+
[]int{0, 0, 0, 1, 1, 1, 2, 2, 2},
3636
},
3737
},
38-
38+
3939
// 如需多个测试,可以复制上方元素。
4040
}
4141

4242
for _, q := range qs {
4343
a, p := q.ans, q.para
4444
fmt.Printf("~~%v~~\n", p)
45-
46-
ast.Equal(a.one, sortColors(p. ), "输入:%v", p)
45+
sortColors(p.nums)
46+
ast.Equal(a.one, p.nums, "输入:%v", p)
4747
}
4848
}

0 commit comments

Comments
 (0)