Skip to content

Commit 8d66caf

Browse files
committed
udpate
1 parent 95f6645 commit 8d66caf

9 files changed

+681
-2
lines changed

004.单调栈.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func main() {
1919

2020
/*
2121
1. 下一个, 从前向后遍历
22-
上一个, 从后向前遍历
22+
上一个, 从后向前遍历
2323
2. smarller 弹出比v大的
24-
2. larger 弹出比v小的
24+
larger 弹出比v小的
2525
2626
*/
2727

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @lc app=leetcode.cn id=1013 lang=golang
3+
*
4+
* [1013] 将数组分成和相等的三个部分
5+
*
6+
* https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/description/
7+
*
8+
* algorithms
9+
* Easy (39.19%)
10+
* Likes: 172
11+
* Dislikes: 0
12+
* Total Accepted: 51.7K
13+
* Total Submissions: 131.9K
14+
* Testcase Example: '[0,2,1,-6,6,-7,9,1,2,0,1]'
15+
*
16+
* 给你一个整数数组 arr,只有可以将其划分为三个和相等的 非空 部分时才返回 true,否则返回 false。
17+
*
18+
* 形式上,如果可以找出索引 i + 1 < j 且满足 (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] +
19+
* arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length
20+
* - 1]) 就可以将数组三等分。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
* 输入:arr = [0,2,1,-6,6,-7,9,1,2,0,1]
28+
* 输出:true
29+
* 解释:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
30+
*
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:arr = [0,2,1,-6,6,7,9,-1,2,0,1]
36+
* 输出:false
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
*
42+
* 输入:arr = [3,3,6,5,-2,2,5,1,-9,4]
43+
* 输出:true
44+
* 解释:3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
45+
*
46+
*
47+
*
48+
*
49+
* 提示:
50+
*
51+
*
52+
* 3
53+
* -10^4
54+
*
55+
*
56+
*/
57+
58+
// @lc code=start
59+
func canThreePartsEqualSum(arr []int) bool {
60+
sum := 0
61+
for _, v := range arr {
62+
sum += v
63+
}
64+
if sum%3 != 0 {
65+
return false
66+
}
67+
68+
aThird := sum / 3
69+
70+
i := 0
71+
cur := 0
72+
for i < len(arr) {
73+
cur += arr[i]
74+
if cur == aThird {
75+
break
76+
}
77+
i++
78+
}
79+
j := i + 1
80+
81+
for j < len(arr) {
82+
cur += arr[j]
83+
if cur == aThird*2 {
84+
break
85+
}
86+
j++
87+
}
88+
89+
// j不能是最后一个index
90+
if j < len(arr)-1 {
91+
return true
92+
}
93+
return false
94+
}
95+
96+
// @lc code=end
97+

1021.删除最外层的括号.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @lc app=leetcode.cn id=1021 lang=golang
3+
*
4+
* [1021] 删除最外层的括号
5+
*
6+
* https://leetcode-cn.com/problems/remove-outermost-parentheses/description/
7+
*
8+
* algorithms
9+
* Easy (78.47%)
10+
* Likes: 197
11+
* Dislikes: 0
12+
* Total Accepted: 62.8K
13+
* Total Submissions: 80.1K
14+
* Testcase Example: '"(()())(())"'
15+
*
16+
* 有效括号字符串为空 ""、"(" + A + ")" 或 A + B ,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。
17+
*
18+
*
19+
* 例如,"","()","(())()" 和 "(()(()))" 都是有效的括号字符串。
20+
*
21+
*
22+
* 如果有效字符串 s 非空,且不存在将其拆分为 s = A + B 的方法,我们称其为原语(primitive),其中 A 和 B
23+
* 都是非空有效括号字符串。
24+
*
25+
* 给出一个非空有效字符串 s,考虑将其进行原语化分解,使得:s = P_1 + P_2 + ... + P_k,其中 P_i 是有效括号字符串原语。
26+
*
27+
* 对 s 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 s 。
28+
*
29+
*
30+
*
31+
* 示例 1:
32+
*
33+
*
34+
* 输入:s = "(()())(())"
35+
* 输出:"()()()"
36+
* 解释:
37+
* 输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
38+
* 删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。
39+
*
40+
* 示例 2:
41+
*
42+
*
43+
* 输入:s = "(()())(())(()(()))"
44+
* 输出:"()()()()(())"
45+
* 解释:
46+
* 输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
47+
* 删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。
48+
*
49+
*
50+
* 示例 3:
51+
*
52+
*
53+
* 输入:s = "()()"
54+
* 输出:""
55+
* 解释:
56+
* 输入字符串为 "()()",原语化分解得到 "()" + "()",
57+
* 删除每个部分中的最外层括号后得到 "" + "" = ""。
58+
*
59+
*
60+
*
61+
*
62+
* 提示:
63+
*
64+
*
65+
* 1
66+
* s[i] 为 '(' 或 ')'
67+
* s 是一个有效括号字符串
68+
*
69+
*
70+
*/
71+
72+
// 输入一定是合法的括号组合
73+
// @lc code=start
74+
func removeOuterParentheses(s string) string {
75+
76+
lnum := 0
77+
stack := make([]byte, 0)
78+
ans := make([]byte, 0)
79+
for i := range s {
80+
char := s[i]
81+
if char == byte('(') {
82+
lnum++
83+
} else {
84+
lnum--
85+
}
86+
stack = append(stack, char)
87+
if lnum == 0 && len(stack) > 0 {
88+
ans = append(ans, stack[1:len(stack)-1]...)
89+
stack = []byte{}
90+
}
91+
}
92+
return string(ans)
93+
}
94+
95+
// @lc code=end
96+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* @lc app=leetcode.cn id=1022 lang=golang
3+
*
4+
* [1022] 从根到叶的二进制数之和
5+
*
6+
* https://leetcode-cn.com/problems/sum-of-root-to-leaf-binary-numbers/description/
7+
*
8+
* algorithms
9+
* Easy (71.27%)
10+
* Likes: 141
11+
* Dislikes: 0
12+
* Total Accepted: 23.6K
13+
* Total Submissions: 33.1K
14+
* Testcase Example: '[1,0,1,0,1,0,1]'
15+
*
16+
* 给出一棵二叉树,其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。
17+
*
18+
*
19+
* 例如,如果路径为 0 -> 1 -> 1 -> 0 -> 1,那么它表示二进制数 01101,也就是 13 。
20+
*
21+
*
22+
* 对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。
23+
*
24+
* 返回这些数字之和。题目数据保证答案是一个 32 位 整数。
25+
*
26+
*
27+
*
28+
* 示例 1:
29+
*
30+
*
31+
* 输入:root = [1,0,1,0,1,0,1]
32+
* 输出:22
33+
* 解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
*
39+
* 输入:root = [0]
40+
* 输出:0
41+
*
42+
*
43+
*
44+
*
45+
* 提示:
46+
*
47+
*
48+
* 树中的节点数在 [1, 1000] 范围内
49+
* Node.val 仅为 0 或 1
50+
*
51+
*
52+
*/
53+
54+
// @lc code=start
55+
/**
56+
* Definition for a binary tree node.
57+
* type TreeNode struct {
58+
* Val int
59+
* Left *TreeNode
60+
* Right *TreeNode
61+
* }
62+
*/
63+
func sumRootToLeaf(root *TreeNode) int {
64+
65+
ans := 0
66+
var dfs func(*TreeNode, int)
67+
dfs = func(n *TreeNode, pre int) {
68+
// 跟pre<<1 + n.Val 值一样
69+
pre = pre<<1 | n.Val
70+
if n.Left == nil && n.Right == nil {
71+
ans += pre
72+
}
73+
if n.Left != nil {
74+
dfs(n.Left, pre)
75+
}
76+
if n.Right != nil {
77+
dfs(n.Right, pre)
78+
}
79+
}
80+
dfs(root, 0)
81+
return ans
82+
83+
}
84+
85+
// @lc code=end
86+

110.平衡二叉树.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @lc app=leetcode.cn id=110 lang=golang
3+
*
4+
* [110] 平衡二叉树
5+
*
6+
* https://leetcode-cn.com/problems/balanced-binary-tree/description/
7+
*
8+
* algorithms
9+
* Easy (56.79%)
10+
* Likes: 935
11+
* Dislikes: 0
12+
* Total Accepted: 326.6K
13+
* Total Submissions: 574.9K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给定一个二叉树,判断它是否是高度平衡的二叉树。
17+
*
18+
* 本题中,一棵高度平衡二叉树定义为:
19+
*
20+
*
21+
* 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
22+
*
23+
*
24+
*
25+
*
26+
* 示例 1:
27+
*
28+
*
29+
* 输入:root = [3,9,20,null,null,15,7]
30+
* 输出:true
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
*
36+
* 输入:root = [1,2,2,3,3,null,null,4,4]
37+
* 输出:false
38+
*
39+
*
40+
* 示例 3:
41+
*
42+
*
43+
* 输入:root = []
44+
* 输出:true
45+
*
46+
*
47+
*
48+
*
49+
* 提示:
50+
*
51+
*
52+
* 树中的节点数在范围 [0, 5000] 内
53+
* -10^4
54+
*
55+
*
56+
*/
57+
58+
// @lc code=start
59+
/**
60+
* Definition for a binary tree node.
61+
* type TreeNode struct {
62+
* Val int
63+
* Left *TreeNode
64+
* Right *TreeNode
65+
* }
66+
*/
67+
func isBalanced(root *TreeNode) bool {
68+
if root == nil {
69+
return true
70+
}
71+
return isBalanced(root.Left) && isBalanced(root.Right) && abs(height(root.Left)-height(root.Right)) <= 1
72+
}
73+
74+
func height(n *TreeNode) int {
75+
if n == nil {
76+
return 0
77+
}
78+
return max(height(n.Left), height(n.Right)) + 1
79+
}
80+
81+
func max(x, y int) int {
82+
if x > y {
83+
return x
84+
}
85+
return y
86+
}
87+
88+
func abs(x int) int {
89+
if x < 0 {
90+
return -1 * x
91+
}
92+
return x
93+
}
94+
95+
// @lc code=end
96+

0 commit comments

Comments
 (0)