Skip to content

Commit 10dfddf

Browse files
author
Kyle Liu
committed
add some solution for jz offer
1 parent f87ba6e commit 10dfddf

22 files changed

+846
-1
lines changed

lcof/of027/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of027/Solution.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func mirrorTree(root *TreeNode) *TreeNode {
10+
if root != nil {
11+
root.Left, root.Right = mirrorTree(root.Right), mirrorTree(root.Left)
12+
}
13+
return root
14+
}

lcof/of027/Solution_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func(*TreeNode) *TreeNode
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
mirrorTree,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
root *TreeNode
21+
expect *TreeNode
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
root: &TreeNode{
29+
Val: 6,
30+
Left: &TreeNode{
31+
Val: 2,
32+
Left: &TreeNode{Val: 0},
33+
Right: &TreeNode{
34+
Val: 4,
35+
Left: &TreeNode{Val: 3},
36+
Right: &TreeNode{Val: 5},
37+
},
38+
},
39+
Right: &TreeNode{
40+
Val: 8,
41+
Left: &TreeNode{Val: 7},
42+
Right: &TreeNode{Val: 9},
43+
},
44+
},
45+
expect: &TreeNode{
46+
Val: 6,
47+
Left: &TreeNode{
48+
Val: 8,
49+
Left: &TreeNode{Val: 9},
50+
Right: &TreeNode{Val: 7},
51+
},
52+
Right: &TreeNode{
53+
Val: 2,
54+
Left: &TreeNode{
55+
Val: 4,
56+
Left: &TreeNode{Val: 5},
57+
Right: &TreeNode{Val: 3},
58+
},
59+
Right: &TreeNode{Val: 0},
60+
},
61+
},
62+
},
63+
}
64+
65+
// TestSolution Example for solution test cases
66+
func TestSolution(t *testing.T) {
67+
ast := assert.New(t)
68+
69+
for _, f := range SolutionFuncList {
70+
for _, c := range cases {
71+
t.Run(c.name, func(t *testing.T) {
72+
actual := f(c.root)
73+
ast.Equal(c.expect, actual,
74+
"func: %v case: %v ",
75+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
76+
})
77+
}
78+
}
79+
}

lcof/of028/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of028/Solution.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func isSymmetric(root *TreeNode) bool {
10+
return dfs(root, root)
11+
}
12+
13+
func dfs(t1 *TreeNode, t2 *TreeNode) bool {
14+
if t1 == nil && t2 == nil {
15+
return true
16+
} else if t1 == nil || t2 == nil {
17+
return false
18+
}
19+
return t1.Val == t2.Val && dfs(t1.Right, t2.Left) && dfs(t1.Left, t2.Right)
20+
}

lcof/of028/Solution_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func(*TreeNode) bool
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
isSymmetric,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
root *TreeNode
21+
expect bool
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
root: &TreeNode{
29+
Val: 1,
30+
Left: &TreeNode{
31+
Val: 2,
32+
Right: &TreeNode{
33+
Val: 3,
34+
},
35+
},
36+
Right: &TreeNode{
37+
Val: 2,
38+
Right: &TreeNode{
39+
Val: 3,
40+
},
41+
},
42+
},
43+
expect: false,
44+
},
45+
}
46+
47+
// TestSolution Example for solution test cases
48+
func TestSolution(t *testing.T) {
49+
ast := assert.New(t)
50+
51+
for _, f := range SolutionFuncList {
52+
for _, c := range cases {
53+
t.Run(c.name, func(t *testing.T) {
54+
actual := f(c.root)
55+
ast.Equal(c.expect, actual,
56+
"func: %v case: %v ",
57+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
58+
})
59+
}
60+
}
61+
}

lcof/of032-I/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of032-I/Solution.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func levelOrder(root *TreeNode) []int {
10+
if root == nil {
11+
return []int{}
12+
}
13+
queue, res := make([]*TreeNode, 0), []int{}
14+
queue = append(queue, root)
15+
for len(queue) != 0 {
16+
cRoot := queue[0]
17+
res = append(res, cRoot.Val)
18+
if cRoot.Left != nil {
19+
queue = append(queue, cRoot.Left)
20+
}
21+
if cRoot.Right != nil {
22+
queue = append(queue, cRoot.Right)
23+
}
24+
queue = queue[1:]
25+
}
26+
return res
27+
}

lcof/of032-I/Solution_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func(*TreeNode) []int
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
levelOrder,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
root *TreeNode
21+
expect []int
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
root: &TreeNode{
29+
Val: 3,
30+
Left: &TreeNode{
31+
Val: 9,
32+
},
33+
Right: &TreeNode{
34+
Val: 20,
35+
Left: &TreeNode{Val: 15},
36+
Right: &TreeNode{Val: 7},
37+
},
38+
},
39+
expect: []int{3, 9, 20, 15, 7},
40+
},
41+
}
42+
43+
// TestSolution Example for solution test cases
44+
func TestSolution(t *testing.T) {
45+
ast := assert.New(t)
46+
47+
for _, f := range SolutionFuncList {
48+
for _, c := range cases {
49+
t.Run(c.name, func(t *testing.T) {
50+
actual := f(c.root)
51+
ast.Equal(c.expect, actual,
52+
"func: %v case: %v ",
53+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
54+
})
55+
}
56+
}
57+
}

lcof/of032-II/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

0 commit comments

Comments
 (0)