Skip to content

Commit 8b9eab4

Browse files
committed
Algorithms: Sort Colors
1 parent 2f9646d commit 8b9eab4

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-16%20%2F%202046-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-17%20%2F%202046-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
55

66
### LeetCode Algorithm
@@ -16,6 +16,7 @@
1616
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 |
1717
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position) | 🟢 |
1818
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 |
19+
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Go](go/0075_sort_colors) | 🟡 |
1920
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 |
2021
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 |
2122
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 |

go/0075_sort_colors/solution.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package sort_colors
2+
3+
func sortColors(nums []int) {
4+
numLen := len(nums)
5+
if numLen <= 1 {
6+
return
7+
}
8+
9+
// Swap elements in-place loop
10+
lShift, rShift := 0, 0
11+
for i := 0; i < numLen-rShift; i++ {
12+
if nums[i] == 0 {
13+
nums[i], nums[lShift] = nums[lShift], nums[i]
14+
lShift++
15+
} else if nums[i] == 2 {
16+
nums[i], nums[numLen-rShift-1] = nums[numLen-rShift-1], nums[i]
17+
rShift++
18+
i--
19+
}
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sort_colors
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_sortColors(t *testing.T) {
9+
tests := []struct {
10+
input []int
11+
want []int
12+
}{
13+
{input: []int{2, 0, 2, 1, 1, 0}, want: []int{0, 0, 1, 1, 2, 2}},
14+
{input: []int{2, 0, 2, 1, 1, 1}, want: []int{0, 1, 1, 1, 2, 2}},
15+
{input: []int{2, 2, 2, 1, 1, 1}, want: []int{1, 1, 1, 2, 2, 2}},
16+
{input: []int{1, 2, 2, 0, 1}, want: []int{0, 1, 1, 2, 2}},
17+
{input: []int{0, 1, 2, 2}, want: []int{0, 1, 2, 2}},
18+
{input: []int{1, 2, 0}, want: []int{0, 1, 2}},
19+
{input: []int{2, 0, 1}, want: []int{0, 1, 2}},
20+
{input: []int{2, 2, 2}, want: []int{2, 2, 2}},
21+
{input: []int{1}, want: []int{1}},
22+
{input: []int{0}, want: []int{0}},
23+
{input: []int{}, want: []int{}},
24+
}
25+
26+
for i, tc := range tests {
27+
sortColors(tc.input)
28+
if !reflect.DeepEqual(tc.want, tc.input) {
29+
t.Fatalf("Case #%d: expected: %v, got: %v", i+1, tc.want, tc.input)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)