Skip to content

Commit 96b10a2

Browse files
committed
Merge branch 'master' of github.com:kylesliu/awesome-golang-algorithm
2 parents 9ca3e34 + d95f16b commit 96b10a2

File tree

53 files changed

+976
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+976
-270
lines changed
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) [][]int {
6+
res := [][]int{}
7+
sort.Slice(nums, func(i, j int) bool {
8+
return nums[i] < nums[j]
9+
})
10+
visited := make(map[int]bool)
11+
helper(nums, []int{}, visited, &res)
12+
return res
13+
}
14+
15+
func helper(nums, res []int, visited map[int]bool, final *[][]int) {
16+
if len(nums) == len(res) {
17+
temp := make([]int, len(res))
18+
copy(temp, res)
19+
*final = append(*final, temp)
20+
return
21+
}
22+
for i := 0; i < len(nums); i++ {
23+
if visited[i] {
24+
continue
25+
}
26+
if i > 0 && nums[i] == nums[i - 1] && !visited[i - 1] {
27+
continue
28+
}
29+
res = append(res, nums[i])
30+
visited[i] = true
31+
helper(nums, res, visited, final)
32+
visited[i] = false
33+
res = res[:len(res) - 1]
34+
}
535
}

leetcode/1-100/0047.Permutations-II/Solution_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
inputs []int
13+
expect [][]int
1414
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
15+
{"TestCacse 1", []int{1,1,2}, [][]int{
16+
{1,1,2}, {1,2,1}, {2,1,1},
17+
}},
18+
{"TestCacse 2", []int{1,2,3}, [][]int{
19+
{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1},
20+
}},
1821
}
1922

2023
// 开始测试
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(matrix [][]int) {
4+
n := len(matrix)
5+
for i := 0; i < n; i++ {
6+
for j := i; j < n; j++ {
7+
matrix[j][i], matrix[i][j] = matrix[i][j], matrix[j][i]
8+
}
9+
}
10+
for i := 0; i < n; i++ {
11+
reversed := make([]int, n)
12+
for j := 0; j < n; j++ {
13+
reversed[j] = matrix[i][n - j - 1]
14+
}
15+
matrix[i] = reversed
16+
}
517
}

leetcode/1-100/0048.Rotate-Image/Solution_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,30 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
inputs [][]int
13+
expect [][]int
1414
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
15+
{"TestCase 1", [][]int{
16+
{1,2,3}, {4,5,6}, {7,8,9},
17+
}, [][]int{
18+
{7,4,1}, {8,5,2}, {9,6,3},
19+
}},
20+
{"TestCase 2", [][]int{
21+
{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16},
22+
}, [][]int{
23+
{15,13,2,5},{14,3,4,1},{12,6,8,9},{16,7,10,11},
24+
}},
1825
}
1926

2027
// 开始测试
2128
for _, c := range cases {
2229
t.Run(c.name, func(t *testing.T) {
23-
ret := Solution(c.inputs)
24-
if !reflect.DeepEqual(ret, c.expect) {
30+
input := [][]int{}
31+
copy(input, c.inputs)
32+
Solution(c.inputs)
33+
if !reflect.DeepEqual(c.inputs, c.expect) {
2534
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
35+
c.expect, c.inputs, input)
2736
}
2837
})
2938
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
cmpObj, repeatCount := nums[0], 1
5+
fillIdx := 0
6+
for idx := 1; idx < len(nums); idx++ {
7+
if nums[idx] == cmpObj {
8+
repeatCount++
9+
continue
10+
}
11+
12+
cmpObj = nums[idx]
13+
14+
if repeatCount > 2 {
15+
repeatCount = 2
16+
}
17+
preObj := nums[idx-1]
18+
for ; repeatCount > 0; repeatCount, fillIdx = repeatCount-1, fillIdx+1 {
19+
nums[fillIdx] = preObj
20+
}
21+
repeatCount = 1
22+
}
23+
if repeatCount > 2 {
24+
repeatCount = 2
25+
}
26+
27+
preObj := nums[len(nums)-1]
28+
for ; repeatCount > 0; repeatCount, fillIdx = repeatCount-1, fillIdx+1 {
29+
nums[fillIdx] = preObj
30+
}
31+
32+
return fillIdx
533
}

leetcode/1-100/0080.Remove-Duplicates-from-Sorted-Array-II/Solution_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
inputs []int
13+
expect int
1414
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
15+
{"TestCase1", []int{1}, 1},
16+
{"TestCase2", []int{1, 1, 1, 2, 2, 3}, 5},
17+
{"TestCase3", []int{0, 0, 1, 1, 1, 1, 2, 3, 3}, 7},
18+
{"TestCase4", []int{1, 2, 3, 4}, 4},
1819
}
1920

2021
// 开始测试
@@ -25,6 +26,7 @@ func TestSolution(t *testing.T) {
2526
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2627
c.expect, ret, c.inputs)
2728
}
29+
t.Logf("%v", c.inputs[:c.expect])
2830
})
2931
}
3032
}
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(root *TreeNode) [][]int {
4+
if root == nil {
5+
return [][]int{}
6+
}
7+
res := make([][]int, 0)
8+
stack := []*TreeNode{root}
9+
reverse := false
10+
for len(stack) > 0 {
11+
temp := make([]*TreeNode, 0)
12+
tempVals := make([]int, 0)
13+
for len(stack) > 0 {
14+
node := stack[0]
15+
tempVals = append(tempVals, node.Val)
16+
if node.Left != nil {
17+
temp = append(temp, node.Left)
18+
}
19+
if node.Right != nil {
20+
temp = append(temp, node.Right)
21+
}
22+
stack = stack[1:]
23+
}
24+
if reverse {
25+
for i, j := 0, len(tempVals) - 1; i < j; i, j = i + 1, j - 1 {
26+
tempVals[i], tempVals[j] = tempVals[j], tempVals[i]
27+
}
28+
}
29+
reverse = !reverse
30+
res = append(res, tempVals)
31+
stack = append(stack, temp...)
32+
}
33+
return res
534
}

leetcode/101-200/0103.Binary-Tree-Zigzag-Level-Order-Traversal/Solution_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", &TreeNode{3, &TreeNode{9, nil, nil}, &TreeNode{20, &TreeNode{15, nil, nil}, &TreeNode{7, nil, nil}}}, [][]int{
17+
{3}, {20, 9}, {15,7},
18+
}},
19+
{"TestCase", &TreeNode{1, nil, nil}, [][]int{{1}}},
20+
{"TestCase", nil, [][]int{}},
1921
}
2022

2123
// 开始测试
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
var idxMap map[int]int
4+
var preorderIdx int
5+
6+
func Solution(preorder []int, inorder []int) *TreeNode {
7+
idxMap = make(map[int]int)
8+
preorderIdx = 0
9+
for i, v := range inorder {
10+
idxMap[v] = i
11+
}
12+
return build(0, len(inorder) - 1, preorder)
13+
}
14+
15+
func build(left, right int, preorder []int) *TreeNode {
16+
if left > right {
17+
return nil
18+
}
19+
root := &TreeNode{Val: preorder[preorderIdx]}
20+
preorderIdx++
21+
idx := idxMap[root.Val]
22+
root.Left = build(left, idx - 1, preorder)
23+
root.Right = build(idx + 1, right, preorder)
24+
return root
525
}

leetcode/101-200/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
preorder []int
14+
inorder []int
15+
expect *TreeNode
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{3,9,20,15,7}, []int{9,3,15,20,7}, &TreeNode{3, &TreeNode{Val: 9}, &TreeNode{20, &TreeNode{Val: 15}, &TreeNode{Val: 7}}}},
18+
{"TestCase", []int{-1}, []int{-1}, &TreeNode{Val: -1}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.preorder, c.inorder)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.preorder, c.inorder)
2828
}
2929
})
3030
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
var idxMap map[int]int
4+
var p int
5+
6+
func Solution(inorder []int, postorder []int) *TreeNode {
7+
p = len(postorder) - 1
8+
idxMap = make(map[int]int)
9+
for i, v := range inorder {
10+
idxMap[v] = i
11+
}
12+
return build(0, len(inorder) - 1, postorder)
13+
}
14+
15+
func build(left, right int, postorder []int) *TreeNode {
16+
if left > right {
17+
return nil
18+
}
19+
root := &TreeNode{Val: postorder[p]}
20+
p--
21+
idx := idxMap[root.Val]
22+
root.Right = build(idx + 1, right, postorder)
23+
root.Left = build(left, idx - 1, postorder)
24+
return root
525
}

leetcode/101-200/0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
postorder []int
14+
inorder []int
15+
expect *TreeNode
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{9,15,7,20,3}, []int{9,3,15,20,7}, &TreeNode{3, &TreeNode{Val: 9}, &TreeNode{20, &TreeNode{Val: 15}, &TreeNode{Val: 7}}}},
18+
{"TestCase", []int{-1}, []int{-1}, &TreeNode{Val: -1}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inorder, c.postorder)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inorder, c.postorder)
2828
}
2929
})
3030
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}

0 commit comments

Comments
 (0)