Skip to content

Commit 3c7b803

Browse files
author
陈世伟
committed
feat:2021年05月26日14:49:20
1 parent 571483f commit 3c7b803

File tree

15 files changed

+543
-2
lines changed

15 files changed

+543
-2
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
package _047_删除字符串中的所有相邻重复项_stack_
1+
package _047_删除字符串中的所有相邻重复项
2+
3+
import "fmt"
24

35
func removeDuplicates(S string) string {
46
var stack []byte
57
for i := 0; i < len(S); i++ {
68
if len(stack) > 0 && stack[len(stack)-1] == S[i] {
9+
fmt.Println(stack[len(stack)-1], S[i], string(stack[:len(stack)-1]))
710
stack = stack[:len(stack)-1]
811
continue
912
}
1013
stack = append(stack, S[i])
1114
}
15+
fmt.Println(string(stack))
1216
return string(stack)
1317
}

leetcode/1047.删除字符串中的所有相邻重复项[stack]/remove_duplicates_test.go renamed to leetcode/1047.删除字符串中的所有相邻重复项/remove_duplicates_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _047_删除字符串中的所有相邻重复项_stack_
1+
package _047_删除字符串中的所有相邻重复项
22

33
import "testing"
44

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package _7_电话号码的字母组合
2+
3+
var letterMap = []string{
4+
"", // 0
5+
"", // 1
6+
"abc", // 2
7+
"def", // 3
8+
"ghi", // 4
9+
"jkl", // 5
10+
"mno", // 6
11+
"pqrs", // 7
12+
"tuv", // 8
13+
"wxyz", // 9
14+
}
15+
16+
func letterCombinations(digits string) []string {
17+
var (
18+
paths []string
19+
path []byte
20+
fn func(digits string, startIdx int)
21+
)
22+
if len(digits) == 0 {
23+
return nil
24+
}
25+
26+
fn = func(digits string, startIdx int) {
27+
if len(path) == len(digits) {
28+
tmp := make([]byte, len(digits), len(digits))
29+
copy(tmp, path)
30+
paths = append(paths, string(tmp))
31+
return
32+
}
33+
34+
world := letterMap[digits[startIdx]-'0']
35+
// for 横向遍历
36+
for _, v := range world {
37+
path = append(path, byte(v))
38+
// 递归纵向遍历
39+
fn(digits, startIdx+1)
40+
// 回溯不断调整结果集
41+
path = path[:len(path)-1]
42+
}
43+
44+
}
45+
46+
fn(digits, 0)
47+
48+
return paths
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package _7_电话号码的字母组合
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_letterCombinations(t *testing.T) {
9+
type args struct {
10+
digits string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want []string
16+
}{
17+
{name: `输入:digits = "23"
18+
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]`, args: args{digits: "23"}, want: []string{
19+
"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf",
20+
}},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := letterCombinations(tt.args.digits); !reflect.DeepEqual(got, tt.want) {
25+
t.Errorf("letterCombinations() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _16_组合总和III
2+
3+
func combinationSum3(k int, n int) [][]int {
4+
5+
var (
6+
fn func(k, n, startIdx int)
7+
paths [][]int
8+
path []int
9+
// 计算sum
10+
sum int
11+
)
12+
fn = func(k, n, startIdx int) {
13+
if len(path) == k { // 符合3个数
14+
if sum == n { // 判断总和
15+
tmp := make([]int, len(path), cap(path))
16+
copy(tmp, path)
17+
paths = append(paths, tmp)
18+
}
19+
return
20+
}
21+
for i := startIdx; i <= 9-(k-len(path))+1; i++ {
22+
path = append(path, i)
23+
sum += i
24+
fn(k, n, i+1)
25+
path = path[:len(path)-1]
26+
sum -= i
27+
}
28+
}
29+
30+
fn(k, n, 1)
31+
32+
return paths
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _16_组合总和III
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_combinationSum3(t *testing.T) {
9+
type args struct {
10+
k int
11+
n int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want [][]int
17+
}{
18+
{name: `输入: k = 3, n = 7 输出: [[1,2,4]]`, args: args{
19+
k: 3,
20+
n: 7,
21+
}, want: [][]int{{1, 2, 4}}},
22+
}
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
if got := combinationSum3(tt.args.k, tt.args.n); !reflect.DeepEqual(got, tt.want) {
26+
t.Errorf("combinationSum3() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package _9_组合总和
2+
3+
func combinationSum(candidates []int, target int) [][]int {
4+
var (
5+
paths [][]int
6+
path []int
7+
sum int
8+
fn func(candidates []int, target int, startIdx int)
9+
)
10+
11+
fn = func(candidates []int, target int, startIdx int) {
12+
if sum > target {
13+
return
14+
}
15+
16+
if sum == target {
17+
tmp := make([]int, len(path))
18+
copy(tmp, path)
19+
paths = append(paths, tmp)
20+
return
21+
}
22+
23+
for i := startIdx; i < len(candidates); i++ {
24+
path = append(path, candidates[i])
25+
sum += candidates[i]
26+
fn(candidates, target, i)
27+
path = path[:len(path)-1]
28+
sum -= candidates[i]
29+
}
30+
}
31+
32+
fn(candidates, target, 0)
33+
34+
return paths
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _9_组合总和
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_combinationSum(t *testing.T) {
9+
type args struct {
10+
candidates []int
11+
target int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want [][]int
17+
}{
18+
{name: `输入:candidates = [2,3,6,7], target = 7,
19+
所求解集为:
20+
[
21+
[7],
22+
[2,2,3]
23+
]`, args: args{
24+
candidates: []int{2, 3, 6, 7},
25+
target: 7,
26+
}, want: [][]int{
27+
{2, 2, 3},
28+
{7},
29+
}},
30+
}
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
if got := combinationSum(tt.args.candidates, tt.args.target); !reflect.DeepEqual(got, tt.want) {
34+
t.Errorf("combinationSum() = %v, want %v", got, tt.want)
35+
}
36+
})
37+
}
38+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package _0_组合总和II
2+
3+
import "sort"
4+
5+
func combinationSum2(candidates []int, target int) [][]int {
6+
7+
var (
8+
use = make([]bool, len(candidates))
9+
paths [][]int
10+
path []int
11+
fn func(candidates []int, target int, startIdx int)
12+
sum int
13+
)
14+
sort.Ints(candidates)
15+
16+
fn = func(candidates []int, target int, startIdx int) {
17+
if sum == target {
18+
tmp := make([]int, len(path))
19+
copy(tmp, path)
20+
paths = append(paths, tmp)
21+
return
22+
}
23+
24+
if sum > target || startIdx >= len(candidates) {
25+
return
26+
}
27+
28+
for i := startIdx; i < len(candidates); i++ {
29+
// 判断树层是够被使用 false
30+
if i > 0 && candidates[i] == candidates[i-1] && !use[i-1] {
31+
continue
32+
}
33+
/*树支开始*/
34+
use[i] = true
35+
path = append(path, candidates[i])
36+
sum += candidates[i]
37+
fn(candidates, target, i+1)
38+
/*树支结束*/
39+
/*树层开始*/
40+
use[i] = false
41+
path = path[:len(path)-1]
42+
sum -= candidates[i]
43+
/*树层结束*/
44+
}
45+
46+
}
47+
48+
fn(candidates, target, 0)
49+
50+
return paths
51+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package _0_组合总和II
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_combinationSum2(t *testing.T) {
9+
type args struct {
10+
candidates []int
11+
target int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want [][]int
17+
}{
18+
{name: `输入: candidates = [10,1,2,7,6,1,5], target = 8,
19+
所求解集为:
20+
[
21+
[1, 7],
22+
[1, 2, 5],
23+
[2, 6],
24+
[1, 1, 6]
25+
]
26+
`, args: args{
27+
candidates: []int{10, 1, 2, 7, 6, 1, 5},
28+
target: 8,
29+
}, want: [][]int{
30+
{1, 7},
31+
{1, 2, 5},
32+
{2, 6},
33+
{1, 1, 6},
34+
}},
35+
}
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
if got := combinationSum2(tt.args.candidates, tt.args.target); !reflect.DeepEqual(got, tt.want) {
39+
t.Errorf("combinationSum2() = %v, want %v", got, tt.want)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)