Skip to content

Commit 486e096

Browse files
committed
add some tree solution
1 parent 233d2c4 commit 486e096

File tree

76 files changed

+2271
-1913
lines changed

Some content is hidden

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

76 files changed

+2271
-1913
lines changed

lcof/of042/README.md

-32
This file was deleted.

lcof/of042/Solution.go

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,57 @@
11
package Solution
22

3-
func maxSubArray(nums []int) int {
3+
// 动态规划1
4+
func maxSubArray_1(nums []int) int {
5+
if len(nums) == 1 {
6+
return nums[0]
7+
}
8+
dp := make([]int, len(nums))
9+
dp[0] = nums[0]
10+
ans := dp[0]
11+
for i := 1; i < len(nums); i++ {
12+
dp[i] = max(dp[i-1]+nums[i], nums[i])
13+
ans = max(ans, dp[i])
14+
}
15+
return ans
16+
}
17+
18+
// 动态规划2
19+
func maxSubArray_2(nums []int) int {
420
ans := nums[0]
521
for i := 1; i < len(nums); i++ {
6-
if nums[i-1] > 0 {
7-
nums[i] += nums[i-1]
8-
}
22+
nums[i] = max(nums[i-1]+nums[i], nums[i])
923
ans = max(ans, nums[i])
1024
}
1125
return ans
1226
}
1327

28+
// 分治
29+
func maxSubArray_3(nums []int) int {
30+
return get(nums, 0, len(nums)-1).mSum
31+
}
32+
33+
func pushUp(l, r Status) Status {
34+
iSum := l.iSum + r.iSum
35+
lSum := max(l.lSum, l.iSum+r.lSum)
36+
rSum := max(r.rSum, r.iSum+l.rSum)
37+
mSum := max(max(l.mSum, r.mSum), l.rSum+r.lSum)
38+
return Status{lSum, rSum, mSum, iSum}
39+
}
40+
41+
func get(nums []int, l, r int) Status {
42+
if l == r {
43+
return Status{nums[l], nums[l], nums[l], nums[l]}
44+
}
45+
m := (l + r) >> 1
46+
lSub := get(nums, l, m)
47+
rSub := get(nums, m+1, r)
48+
return pushUp(lSub, rSub)
49+
}
50+
51+
type Status struct {
52+
lSum, rSum, mSum, iSum int
53+
}
54+
1455
func max(x, y int) int {
1556
if x > y {
1657
return x

lcof/of042/Solution_test.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package Solution
22

33
import (
4+
"fmt"
45
"reflect"
56
"runtime"
7+
"strings"
68
"testing"
79

810
"github.com/stretchr/testify/assert"
@@ -12,10 +14,12 @@ import (
1214
type SolutionFuncType func([]int) int
1315

1416
var SolutionFuncList = []SolutionFuncType{
15-
maxSubArray,
17+
maxSubArray_1,
18+
maxSubArray_2,
19+
maxSubArray_3,
1620
}
1721

18-
// test info struct
22+
// Test info struct
1923
type Case struct {
2024
name string
2125
inputs []int
@@ -24,24 +28,23 @@ type Case struct {
2428

2529
// test case
2630
var cases = []Case{
27-
{
28-
name: "TestCase 1",
29-
inputs: []int{-2, 1, -3, 4, -1, 2, 1, -5, 4},
30-
expect: 6,
31-
},
31+
{name: "TestCase 1", inputs: []int{1}, expect: 1},
32+
{name: "TestCase 2", inputs: []int{-2, 1}, expect: 1},
33+
{name: "TestCase 3", inputs: []int{-2, -1}, expect: -1},
34+
{name: "TestCase 4", inputs: []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}, expect: 6},
3235
}
3336

3437
// TestSolution Example for solution test cases
3538
func TestSolution(t *testing.T) {
3639
ast := assert.New(t)
3740

3841
for _, f := range SolutionFuncList {
42+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
3943
for _, c := range cases {
40-
t.Run(c.name, func(t *testing.T) {
41-
actual := f(c.inputs)
42-
ast.Equal(c.expect, actual,
43-
"func: %v case: %v ",
44-
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
44+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
45+
got := f(c.inputs)
46+
ast.Equal(c.expect, got,
47+
"func: %v case: %v ", funcName, c.name)
4548
})
4649
}
4750
}

lcof/of053-II/Solution.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Solution
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func missingNumber_1(nums []int) int {
8+
sort.Ints(nums)
9+
if nums[0] != 0 {
10+
return 0
11+
} else if nums[len(nums)-1] != len(nums) {
12+
return len(nums)
13+
}
14+
for i := 1; i < len(nums); i++ {
15+
if i != nums[i] {
16+
return i
17+
}
18+
}
19+
return 0
20+
}
21+
22+
func missingNumber_2(nums []int) int {
23+
m := make(map[int]int)
24+
for _, v := range nums {
25+
m[v]++
26+
}
27+
for i := range nums {
28+
if _, ok := m[i+1]; !ok {
29+
return i + 1
30+
}
31+
}
32+
return 0
33+
}
34+
35+
func missingNumber_3(nums []int) int {
36+
ans := len(nums)
37+
for i, v := range nums {
38+
ans ^= i ^ v
39+
}
40+
return ans
41+
}
42+
43+
func missingNumber_4(nums []int) int {
44+
sum := len(nums) * (len(nums) + 1) / 2
45+
for _, v := range nums {
46+
sum -= v
47+
}
48+
return sum
49+
}

lcof/of053-II/Solution_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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) int
15+
16+
var SolutionFuncList = []SolutionFuncType{
17+
missingNumber_1,
18+
missingNumber_2,
19+
missingNumber_3,
20+
missingNumber_4,
21+
}
22+
23+
// Test case info struct
24+
type Case struct {
25+
name string
26+
input []int
27+
expect int
28+
}
29+
30+
// Test case
31+
var cases = []Case{
32+
{"TestCase 1", []int{3, 0, 1}, 2},
33+
{"TestCase 2", []int{9, 6, 4, 2, 3, 5, 7, 0, 1}, 8},
34+
{"TestCase 2", []int{0, 1}, 2},
35+
}
36+
37+
// TestSolution Run test case for all solutions
38+
func TestSolution(t *testing.T) {
39+
ast := assert.New(t)
40+
41+
for _, f := range SolutionFuncList {
42+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
43+
for _, c := range cases {
44+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
45+
nums := make([]int, 0)
46+
for _, v := range c.input {
47+
nums = append(nums, v)
48+
}
49+
got := f(nums)
50+
ast.Equal(c.expect, got,
51+
"func: %v case: %v ", funcName, c.name)
52+
})
53+
}
54+
}
55+
}

lcof/of054/README.md

-32
This file was deleted.

lcof/of054/Solution.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ type TreeNode struct {
1010
Right *TreeNode
1111
}
1212

13-
func kthLargest(root *TreeNode, k int) int {
14-
var nums []int
15-
nums = dfs(&nums, root)
16-
return nums[k-1]
17-
}
13+
func kthLargest_1(root *TreeNode, k int) int {
14+
ans := -1
15+
var dfs func(node *TreeNode)
16+
dfs = func(node *TreeNode) {
17+
if node == nil {
18+
return
19+
}
20+
dfs(node.Right)
21+
if k == 0 {
22+
return
23+
}
24+
k--
25+
if k == 0 {
26+
ans = node.Val
27+
}
28+
dfs(node.Left)
1829

19-
// 逆中序遍历(右中左 -- 递减序列)
20-
func dfs(nums *[]int, r *TreeNode) []int {
21-
if r.Right != nil {
22-
dfs(nums, r.Right)
23-
}
24-
if r != nil {
25-
*nums = append(*nums, r.Val)
26-
}
27-
if r.Left != nil {
28-
dfs(nums, r.Left)
2930
}
30-
return *nums
31+
dfs(root)
32+
return ans
3133
}

lcof/of054/Solution_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type SolutionFuncType func(*TreeNode, int) int
1313

1414
var SolutionFuncList = []SolutionFuncType{
15-
kthLargest,
15+
kthLargest_1,
1616
}
1717

1818
// test info struct

leetcode/1-100/0094.Binary-Tree-Inorder-Traversal/Solution.go

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

9-
func inorderTraversal(root *TreeNode) []int {
9+
func inorderTraversal_1(root *TreeNode) []int {
1010
ans := make([]int, 0)
1111
if root != nil {
12-
ans = append(ans, inorderTraversal(root.Left)...)
12+
ans = append(ans, inorderTraversal_1(root.Left)...)
1313
ans = append(ans, root.Val)
14-
ans = append(ans, inorderTraversal(root.Right)...)
14+
ans = append(ans, inorderTraversal_1(root.Right)...)
1515
}
1616
return ans
1717
}
1818

1919
func inorderTraversal_2(root *TreeNode) []int {
20-
ans, stack := make([]int, 0), []*TreeNode{}
21-
for root != nil || len(stack) > 0 {
22-
for root != nil {
23-
stack = append(stack, root)
24-
root = root.Left
20+
ans, stk := make([]int, 0), []*TreeNode{}
21+
node := root
22+
for node != nil || len(stk) > 0 {
23+
for node != nil {
24+
stk = append(stk, node)
25+
node = node.Left
2526
}
26-
root = stack[len(stack)-1]
27-
stack = stack[:len(stack)-1]
28-
ans = append(ans, root.Val)
29-
root = root.Right
27+
node = stk[len(stk)-1]
28+
stk = stk[:len(stk)-1]
29+
ans = append(ans, node.Val)
30+
node = node.Right
3031
}
3132
return ans
3233
}

0 commit comments

Comments
 (0)