Skip to content

Commit 23e596d

Browse files
author
baxiang
committed
Merge branch 'master' of github.com:baxiang/leetcode-go
2 parents 3488f3f + 7a40eed commit 23e596d

File tree

2 files changed

+116
-0
lines changed
  • 598range-addition-ii
  • 606construct-string-from-binary-tree

2 files changed

+116
-0
lines changed

598range-addition-ii/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func maxCount1(m int, n int, ops [][]int) int {
6+
if len(ops) == 0 {
7+
return m * n
8+
}
9+
matrix := make([][]int, m)
10+
for i := range matrix {
11+
matrix[i] = make([]int, n)
12+
}
13+
14+
for i := 0; i < len(ops); i++ {
15+
for j := 0; j < ops[i][0]; j++ {
16+
for k := 0; k < ops[i][1]; k++ {
17+
matrix[j][k]++
18+
}
19+
}
20+
}
21+
max := matrix[0][0]
22+
var res int
23+
for i := 0; i < m; i++ {
24+
for j := 0; j < n; j++ {
25+
if max == matrix[i][j] {
26+
res++
27+
}
28+
}
29+
}
30+
return res
31+
}
32+
33+
func maxCount(m int, n int, ops [][]int) int {
34+
if len(ops) == 0 {
35+
return m * n
36+
}
37+
r := ops[0][0]
38+
c := ops[0][1]
39+
for i := 1; i < len(ops); i++ {
40+
if r > ops[i][0] {
41+
r = ops[i][0]
42+
}
43+
if c > ops[i][1] {
44+
c = ops[i][1]
45+
}
46+
}
47+
return r * c
48+
}
49+
50+
func main() {
51+
fmt.Println(maxCount1(3, 3, [][]int{{2, 2}, {3, 3}}))
52+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type TreeNode struct {
9+
Val int
10+
Left *TreeNode
11+
Right *TreeNode
12+
}
13+
14+
func tree2str(t *TreeNode) string {
15+
var str strings.Builder
16+
if t == nil {
17+
return ""
18+
}
19+
stack := []interface{}{t}
20+
for len(stack) > 0 {
21+
node := stack[len(stack)-1]
22+
stack = stack[:len(stack)-1]
23+
if n, ok := node.(*TreeNode); ok {
24+
str.WriteString(fmt.Sprintf("%d", n.Val))
25+
if n.Right != nil {
26+
stack = append(stack, ")")
27+
stack = append(stack, n.Right)
28+
stack = append(stack, "(")
29+
}
30+
if n.Right != nil && n.Left == nil {
31+
stack = append(stack, "()")
32+
}
33+
if n.Left != nil {
34+
stack = append(stack, ")")
35+
stack = append(stack, n.Left)
36+
stack = append(stack, "(")
37+
}
38+
} else {
39+
s := node.(string)
40+
str.WriteString(s)
41+
}
42+
}
43+
return str.String()
44+
}
45+
func main() {
46+
one := &TreeNode{Val: 1}
47+
two := &TreeNode{Val: 2}
48+
one.Left = two
49+
three := &TreeNode{Val: 3}
50+
one.Right = three
51+
four := &TreeNode{Val: 4}
52+
two.Right = four
53+
fmt.Println(tree2str(one))
54+
55+
//one1 :=&TreeNode{Val: 1}
56+
//two1 :=&TreeNode{Val: 2}
57+
//one1.Left = two1
58+
//three1 :=&TreeNode{Val: 3}
59+
//one1.Right = three1
60+
//four1 :=&TreeNode{Val: 4}
61+
//two1.Right = four1
62+
//fmt.Println(tree2str(one1))
63+
64+
}

0 commit comments

Comments
 (0)