Skip to content

Commit 68cbcf4

Browse files
committed
update
1 parent 28d7ced commit 68cbcf4

Some content is hidden

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

97 files changed

+2557
-12
lines changed

000xxx/123.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func max(a, b int) int {
2+
if a < b {
3+
return b
4+
}
5+
return a
6+
}
7+
8+
func maxProfit(prices []int) int {
9+
if 0 == len(prices) {
10+
return 0
11+
}
12+
g, l := [3]int{}, [3]int{}
13+
for i := 0; i < len(prices)-1; i++ {
14+
d := prices[i+1] - prices[i]
15+
for j := 2; j >= 1; j-- {
16+
l[j] = max(g[j-1]+max(d, 0), l[j]+d)
17+
g[j] = max(l[j], g[j])
18+
}
19+
}
20+
return g[2]
21+
}

000xxx/124.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import "math"
2+
3+
func max(a, b int) int {
4+
if a < b {
5+
return b
6+
}
7+
return a
8+
}
9+
10+
func cal(n *TreeNode, o *int) int {
11+
if nil == n {
12+
return 0
13+
}
14+
l := max(cal(n.Left, o), 0)
15+
r := max(cal(n.Right, o), 0)
16+
*o = max(*o, l+r+n.Val)
17+
return max(l, r) + n.Val
18+
}
19+
20+
func maxPathSum(root *TreeNode) int {
21+
o := math.MinInt64
22+
cal(root, &o)
23+
return o
24+
}

000xxx/128.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func longestConsecutive(nums []int) int {
2+
out := 0
3+
m := map[int]int{}
4+
for _, num := range nums {
5+
if _, ok := m[num]; ok {
6+
continue
7+
}
8+
l, r := m[num-1], m[num+1]
9+
s := l + r + 1
10+
m[num], m[num-l], m[num+r] = s, s, s
11+
if s > out {
12+
out = s
13+
}
14+
}
15+
return out
16+
}

000xxx/129.go

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

000xxx/131.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func partition(s string) [][]string {
2+
l := len(s)
3+
out := [][]string{}
4+
ar := []string{}
5+
t := make([][]bool, l)
6+
for i := 0; i < l; i++ {
7+
t[i] = make([]bool, l)
8+
}
9+
for i := 0; i < l; i++ {
10+
for j := 0; j <= i; j++ {
11+
if s[i] == s[j] && (i-j <= 2 || t[j+1][i-1]) {
12+
t[j][i] = true
13+
}
14+
}
15+
}
16+
17+
var cal func(int)
18+
cal = func(b int) {
19+
if b == l {
20+
ar1 := make([]string, len(ar))
21+
copy(ar1, ar)
22+
out = append(out, ar1)
23+
return
24+
}
25+
for i := b; i < l; i++ {
26+
if !t[b][i] {
27+
continue
28+
}
29+
ar = append(ar, s[b:i+1])
30+
cal(i + 1)
31+
ar = ar[:len(ar)-1]
32+
}
33+
}
34+
35+
cal(0)
36+
return out
37+
}

000xxx/132.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import "math"
2+
3+
func min(a, b int) int {
4+
if a < b {
5+
return a
6+
}
7+
return b
8+
}
9+
10+
func minCut(s string) int {
11+
l := len(s)
12+
if 0 == l {
13+
return 0
14+
}
15+
t := make([]int, l+1)
16+
t[0] = -1
17+
for i := 1; i <= l; i++ {
18+
t[i] = math.MaxInt64
19+
}
20+
for i := 0; i < l; i++ {
21+
for j := 0; i-j >= 0 && i+j < l && s[i-j] == s[i+j]; j++ {
22+
t[i+j+1] = min(t[i+j+1], t[i-j]+1)
23+
}
24+
for j := 0; i-j >= 0 && i+j+1 < l && s[i-j] == s[i+j+1]; j++ {
25+
t[i+j+2] = min(t[i+j+2], t[i-j]+1)
26+
}
27+
}
28+
return t[l]
29+
}

000xxx/133.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO
2+
// golang unsuppoted

000xxx/134.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func canCompleteCircuit(gas []int, cost []int) int {
2+
s, b, m := 0, 0, -1
3+
for i := len(gas) - 1; i >= 0; i-- {
4+
s += gas[i] - cost[i]
5+
if s > m {
6+
b, m = i, s
7+
}
8+
}
9+
if s < 0 {
10+
return -1
11+
}
12+
return b
13+
}

000xxx/135.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func max(a, b int) int {
2+
if a < b {
3+
return b
4+
}
5+
return a
6+
}
7+
8+
func candy(ratings []int) int {
9+
l := len(ratings)
10+
t := make([]int, l)
11+
for i := 0; i < l; i++ {
12+
t[i] = 1
13+
}
14+
for i := 0; i < l-1; i++ {
15+
if ratings[i+1] > ratings[i] {
16+
t[i+1] = t[i] + 1
17+
}
18+
}
19+
for i := l - 1; i > 0; i-- {
20+
if ratings[i-1] > ratings[i] {
21+
t[i-1] = max(t[i-1], t[i]+1)
22+
}
23+
}
24+
o := 0
25+
for _, e := range t {
26+
o += e
27+
}
28+
return o
29+
}

000xxx/138.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO
2+
// golang unsuppoted

0 commit comments

Comments
 (0)