Skip to content

Commit 4a33ec9

Browse files
author
chen-shiwei
committed
feat: 回溯 2021年05月26日23:41:38
1 parent 631a2e8 commit 4a33ec9

File tree

6 files changed

+106
-40
lines changed

6 files changed

+106
-40
lines changed

leetcode/0078_todo/subsets.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

leetcode/78.子集/subsets.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _8_子集
2+
3+
func subsets(nums []int) [][]int {
4+
var (
5+
path []int
6+
paths [][]int
7+
fn func(nums []int, startIdx int)
8+
)
9+
10+
fn = func(nums []int, startIdx int) {
11+
tmp := make([]int, len(path))
12+
copy(tmp, path)
13+
paths = append(paths, tmp)
14+
if startIdx >= len(nums) {
15+
//tmp := make([]int, len(path))
16+
//copy(tmp, path)
17+
//paths = append(paths, tmp)
18+
return
19+
}
20+
21+
for i := startIdx; i < len(nums); i++ {
22+
path = append(path, nums[i])
23+
fn(nums, i+1)
24+
path = path[:len(path)-1]
25+
}
26+
}
27+
28+
fn(nums, 0)
29+
30+
return paths
31+
}

leetcode/0078_todo/subsets_test.go renamed to leetcode/78.子集/subsets_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _078_todo
1+
package _8_子集
22

33
import (
44
"reflect"
@@ -33,7 +33,7 @@ func TestSubsets(t *testing.T) {
3333
}
3434
for _, tt := range tests {
3535
t.Run(tt.name, func(t *testing.T) {
36-
if got := Subsets(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
36+
if got := subsets(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
3737
t.Errorf("Subsets() = %v, want %v", got, tt.want)
3838
}
3939
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package _0_子集II
2+
3+
import "sort"
4+
5+
func subsetsWithDup(nums []int) [][]int {
6+
var (
7+
used = make([]bool, len(nums))
8+
path []int
9+
paths [][]int
10+
fn func(nums []int, startIdx int)
11+
)
12+
13+
fn = func(nums []int, startIdx int) {
14+
tmp := make([]int, len(path))
15+
copy(tmp, path)
16+
paths = append(paths, tmp)
17+
if startIdx >= len(nums) {
18+
//tmp := make([]int, len(path))
19+
//copy(tmp, path)
20+
//paths = append(paths, tmp)
21+
return
22+
}
23+
24+
for i := startIdx; i < len(nums); i++ {
25+
if i > 0 && nums[i] == nums[i-1] && !used[i-1] {
26+
continue
27+
}
28+
path = append(path, nums[i])
29+
used[i] = true
30+
fn(nums, i+1)
31+
used[i] = false
32+
path = path[:len(path)-1]
33+
}
34+
}
35+
sort.Ints(nums)
36+
37+
fn(nums, 0)
38+
39+
return paths
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package _0_子集II
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_subsetsWithDup(t *testing.T) {
9+
type args struct {
10+
nums []int
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want [][]int
16+
}{
17+
{name: `输入:nums = [1,2,2]
18+
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]`, args: args{nums: []int{1, 2, 2}}, want: [][]int{
19+
{}, {1}, {1, 2}, {1, 2, 2}, {2}, {2, 2},
20+
}},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := subsetsWithDup(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
25+
t.Errorf("subsetsWithDup() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}

leetcode/93.复原IP地址/restoreIpAddresses.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package _3_复原IP地址
22

3-
import "fmt"
4-
53
func restoreIpAddresses(s string) []string {
64
var (
75
paths []string
@@ -10,24 +8,21 @@ func restoreIpAddresses(s string) []string {
108

119
fn = func(s string, startIdx int, n int) {
1210
if n == 3 {
13-
fmt.Println("==3", n, s, s[startIdx:], isValidIP(s, startIdx, len(s)-1))
1411
if isValidIP(s, startIdx, len(s)-1) {
15-
fmt.Println("append", s)
1612
paths = append(paths, s)
1713
}
1814
return
1915
}
2016

2117
for i := startIdx; i < len(s); i++ {
22-
fmt.Println("isValidIP", string(s[startIdx:i+1]), isValidIP(s, startIdx, i))
2318
if !isValidIP(s, startIdx, i) {
2419
continue
2520
}
26-
s = s[:startIdx+i+1] + "." + s[startIdx+i+1:]
21+
s = s[:i+1] + "." + s[i+1:]
2722
n++
2823
fn(s, i+2, n)
2924
n--
30-
s = s[:startIdx+i+1] + s[startIdx+i+2:]
25+
s = s[:i+1] + s[i+2:]
3126
}
3227
}
3328
fn(s, 0, 0)
@@ -36,7 +31,6 @@ func restoreIpAddresses(s string) []string {
3631

3732
// 有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
3833
func isValidIP(s string, start, end int) bool {
39-
fmt.Println(s, start, end)
4034
if start > end {
4135
return false
4236
}
@@ -46,13 +40,13 @@ func isValidIP(s string, start, end int) bool {
4640
return false
4741
}
4842

49-
var num uint8 = 0
43+
var num = 0
5044
for i := start; i <= end; i++ {
5145
if s[i] > '9' || s[i] < '0' {
5246
return false
5347
}
5448
// 累加
55-
num = num*10 + (s[i] - '0')
49+
num = num*10 + int(s[i]-'0')
5650
if num > 255 {
5751
return false
5852
}

0 commit comments

Comments
 (0)