Skip to content

Commit 898a2e0

Browse files
committed
add some solution
1 parent 486e096 commit 898a2e0

File tree

29 files changed

+829
-600
lines changed

29 files changed

+829
-600
lines changed

lcof/of042/Solution_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type SolutionFuncType func([]int) int
1616
var SolutionFuncList = []SolutionFuncType{
1717
maxSubArray_1,
1818
maxSubArray_2,
19-
maxSubArray_3,
19+
//maxSubArray_3,
2020
}
2121

2222
// Test info struct

lcof/of045/Solution.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strconv"
7+
)
8+
9+
func minNumber(nums []int) string {
10+
ans := ""
11+
sort.Slice(nums, func(i, j int) bool {
12+
s1, s2 := fmt.Sprintf("%d%d", nums[i], nums[j]), fmt.Sprintf("%d%d", nums[j], nums[i])
13+
if s1 > s2 {
14+
return false
15+
}
16+
return true
17+
})
18+
for _, v := range nums {
19+
ans += strconv.Itoa(v)
20+
}
21+
return ans
22+
}

lcof/of045/Solution_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"runtime"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// solution func Info
14+
type SolutionFuncType func([]int) string
15+
16+
var SolutionFuncList = []SolutionFuncType{
17+
minNumber,
18+
}
19+
20+
// test info struct
21+
type Case struct {
22+
name string
23+
inputs []int
24+
expect string
25+
}
26+
27+
// Test case
28+
var cases = []Case{
29+
{name: "TestCase 1", inputs: []int{10, 2}, expect: "102"},
30+
{name: "TestCase 2", inputs: []int{3, 30, 34, 5, 9}, expect: "3033459"},
31+
}
32+
33+
// TestSolution Example for solution test cases
34+
func TestSolution(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, f := range SolutionFuncList {
38+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
39+
for _, c := range cases {
40+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
41+
got := f(c.inputs)
42+
ast.Equal(c.expect, got,
43+
"func: %v case: %v ", funcName, c.name)
44+
})
45+
}
46+
}
47+
}

leetcode/1-100/0003.Longest-Substring-Without-Repeating-Characters/Solution.go

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3+
func lengthOfLongestSubstring_1(s string) int {
4+
// 哈希集合,记录每个字符是否出现过
5+
m := map[byte]int{}
6+
n := len(s)
7+
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
8+
rk, ans := -1, 0
9+
for i := 0; i < n; i++ {
10+
if i != 0 {
11+
// 左指针向右移动一格,移除一个字符
12+
delete(m, s[i-1])
13+
}
14+
for rk+1 < n && m[s[rk+1]] == 0 {
15+
// 不断地移动右指针
16+
m[s[rk+1]]++
17+
rk++
18+
}
19+
// 第 i 到 rk 个字符是一个极长的无重复字符子串
20+
ans = max(ans, rk-i+1)
21+
}
22+
return ans
23+
}
24+
25+
func max(x, y int) int {
26+
if x < y {
27+
return y
28+
}
29+
return x
30+
}
31+
32+
func lengthOfLongestSubstring_2(s string) int {
33+
ans, left, m := 0, 0, map[rune]int{}
34+
for right, v := range s {
35+
left = max(left, m[v])
36+
m[v] = right + 1
37+
ans = max(ans, right-left+1)
38+
}
39+
return ans
40+
}
41+
342
// O(n) time O(1) space Solution
443
func lengthOfLongestSubstring(s string) int {
544
var chPosition [256]int // [0, 0, 0, ...]
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,49 @@
11
package Solution
22

33
import (
4+
"fmt"
45
"reflect"
6+
"runtime"
7+
"strings"
58
"testing"
9+
10+
"github.com/stretchr/testify/assert"
611
)
712

8-
func TestSolution(t *testing.T) {
9-
// 测试用例
10-
cases := []struct {
11-
name string
12-
inputs string
13-
expect int
14-
}{
15-
{"1 test 1", "abcabcbb!", 3},
16-
{"2 test 2", "bbbbb", 1},
17-
{"3 test 3", "pwwkew", 3},
18-
}
13+
// Solution func Info
14+
type SolutionFuncType func(string) int
1915

20-
// 开始测试
21-
for _, c := range cases {
22-
t.Run(c.name, func(t *testing.T) {
23-
ret := lengthOfLongestSubstring(c.inputs)
24-
if !reflect.DeepEqual(ret, c.expect) {
25-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
27-
}
28-
})
29-
}
16+
var SolutionFuncList = []SolutionFuncType{
17+
lengthOfLongestSubstring_1,
18+
lengthOfLongestSubstring_2,
3019
}
3120

32-
func TestSolution2(t *testing.T) {
33-
// 测试用例
34-
cases := []struct {
35-
name string
36-
inputs string
37-
expect int
38-
}{
39-
{"1 test 1", "abcabcbb!", 3},
40-
{"2 test 2", "bbbbb", 1},
41-
{"3 test 3", "pwwkew", 3},
42-
}
43-
44-
// 开始测试
45-
for _, c := range cases {
46-
t.Run(c.name, func(t *testing.T) {
47-
ret := lengthOfLongestSubstring2(c.inputs)
48-
if !reflect.DeepEqual(ret, c.expect) {
49-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
50-
c.expect, ret, c.inputs)
51-
}
52-
})
53-
}
21+
// Test case info struct
22+
type Case struct {
23+
name string
24+
input string
25+
expect int
5426
}
5527

56-
const N = 1000
57-
58-
func BenchmarkMax1(b *testing.B) {
59-
// b.N = 10000
60-
// 测试用例
61-
cases := []struct {
62-
name string
63-
inputs string
64-
expect int
65-
}{
66-
{"1 test 1", "abcabcbb!", 3},
67-
{"2 test 2", "bbbbb", 1},
68-
{"3 test 3", "pwwkew", 3},
69-
}
70-
b.ResetTimer()
71-
for i := 0; i < b.N; i++ {
72-
lengthOfLongestSubstring(cases[0].inputs)
73-
lengthOfLongestSubstring(cases[1].inputs)
74-
lengthOfLongestSubstring(cases[2].inputs)
75-
}
28+
// Test case
29+
var cases = []Case{
30+
{name: "TestCase 1", input: "abcabcbb", expect: 3},
31+
{name: "TestCase 2", input: "bbbbb", expect: 1},
32+
{name: "TestCase 3", input: "pwwkew", expect: 3},
7633
}
7734

78-
func BenchmarkMax2(b *testing.B) {
79-
// b.N = 10000
80-
// 测试用例
81-
cases := []struct {
82-
name string
83-
inputs string
84-
expect int
85-
}{
86-
{"1 test 1", "abcabcbb!", 3},
87-
{"2 test 2", "bbbbb", 1},
88-
{"3 test 3", "pwwkew", 3},
89-
}
90-
b.ResetTimer()
91-
for i := 0; i < b.N; i++ {
92-
lengthOfLongestSubstring2(cases[0].inputs)
93-
lengthOfLongestSubstring2(cases[1].inputs)
94-
lengthOfLongestSubstring2(cases[2].inputs)
35+
// TestSolution Run test case for all solutions
36+
func TestSolution(t *testing.T) {
37+
ast := assert.New(t)
38+
39+
for _, f := range SolutionFuncList {
40+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
41+
for _, c := range cases {
42+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
43+
got := f(c.input)
44+
ast.Equal(c.expect, got,
45+
"func: %v case: %v ", funcName, c.name)
46+
})
47+
}
9548
}
9649
}

leetcode/1-100/0015.3Sum/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sort"
55
)
66

7-
func threeSum(nums []int) [][]int {
7+
func threeSum_1(nums []int) [][]int {
88
ans := [][]int{}
99
if len(nums) <= 2 {
1010
return ans
+36-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
package Solution
22

33
import (
4+
"fmt"
45
"reflect"
5-
"strconv"
6+
"runtime"
7+
"strings"
68
"testing"
9+
10+
"github.com/stretchr/testify/assert"
711
)
812

9-
func TestSolution(t *testing.T) {
10-
// 测试用例
11-
cases := []struct {
12-
name string
13-
inputs []int
14-
expect [][]int
15-
}{
16-
{"TestCase", []int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}}},
17-
{"TestCase", []int{-2, 0, 0, 2, 2}, [][]int{{-2, 0, 2}}},
18-
}
13+
// Solution func Info
14+
type SolutionFuncType func([]int) [][]int
1915

20-
// 开始测试
21-
for i, c := range cases {
22-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
23-
got := threeSum(c.inputs)
24-
if !reflect.DeepEqual(got, c.expect) {
25-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, got, c.inputs)
27-
}
28-
})
29-
}
16+
var SolutionFuncList = []SolutionFuncType{
17+
threeSum_1,
3018
}
3119

32-
// 压力测试
33-
func BenchmarkSolution(b *testing.B) {
20+
// Test case info struct
21+
type Case struct {
22+
name string
23+
input []int
24+
expect [][]int
3425
}
3526

36-
// 使用案列
37-
func ExampleSolution() {
27+
// Test case
28+
var cases = []Case{
29+
{name: "TestCase 1", input: []int{}, expect: [][]int{}},
30+
{name: "TestCase 2", input: []int{0}, expect: [][]int{}},
31+
{name: "TestCase 3", input: []int{-1, 0, 1, 2, -1, -4}, expect: [][]int{{-1, -1, 2}, {-1, 0, 1}}},
32+
{name: "TestCase 4", input: []int{-2, 0, 0, 2, 2}, expect: [][]int{{-2, 0, 2}}},
33+
}
34+
35+
// TestSolution Run test case for all solutions
36+
func TestSolution(t *testing.T) {
37+
ast := assert.New(t)
38+
39+
for _, f := range SolutionFuncList {
40+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
41+
for _, c := range cases {
42+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
43+
got := f(c.input)
44+
ast.Equal(c.expect, got,
45+
"func: %v case: %v ", funcName, c.name)
46+
})
47+
}
48+
}
3849
}

leetcode/101-200/0101.Symmetric-Tree/Solution.go

+3-23
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,6 @@ type TreeNode struct {
66
Right *TreeNode
77
}
88

9-
func isSymmetric_DFS(root *TreeNode) bool {
10-
stack, left, right := []*TreeNode{root.Left, root.Right}, &TreeNode{}, &TreeNode{}
11-
for len(stack) > 0 {
12-
left, right = stack[len(stack)-1], stack[len(stack)-2]
13-
stack = stack[:len(stack)-2]
14-
if left == nil && right == nil {
15-
continue
16-
}
17-
18-
if left == nil || right == nil {
19-
return false
20-
}
21-
22-
if left.Val != right.Val {
23-
return false
24-
}
25-
26-
stack = append(stack, left.Left, right.Right, left.Right, right.Left)
27-
}
28-
return true
29-
}
30-
319
func isSymmetric_1(root *TreeNode) bool {
3210
var dfs func(*TreeNode, *TreeNode) bool
3311
dfs = func(p *TreeNode, q *TreeNode) bool {
@@ -37,7 +15,9 @@ func isSymmetric_1(root *TreeNode) bool {
3715
if p == nil || q == nil {
3816
return false
3917
}
40-
return p.Val == q.Val && dfs(p.Left, q.Right) && dfs(p.Right, q.Left)
18+
return p.Val == q.Val &&
19+
dfs(p.Left, q.Right) &&
20+
dfs(p.Right, q.Left)
4121
}
4222
return dfs(root, root)
4323
}

0 commit comments

Comments
 (0)