Skip to content

Commit 0f2dc2e

Browse files
committed
init
1 parent 1ff8c44 commit 0f2dc2e

Some content is hidden

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

115 files changed

+5368
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

1.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println(twoSum([]int{2, 4, 9, 7, 7, 11, 15}, 9))
9+
}
10+
11+
//给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
12+
//
13+
//你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
14+
//
15+
//示例:
16+
//
17+
//给定 nums = [2, 7, 11, 15], target = 9
18+
//
19+
//因为 nums[0] + nums[1] = 2 + 7 = 9
20+
//所以返回 [0, 1]
21+
22+
// two-sum
23+
// 改进过 一开始两次循环
24+
func twoSum(nums []int, target int) []int {
25+
var result []int
26+
numsMap := make(map[int]int, 0)
27+
for i := range nums {
28+
tmp := target - nums[i]
29+
if _, ok := numsMap[tmp]; ok {
30+
result = []int{numsMap[tmp], i}
31+
}
32+
numsMap[nums[i]] = i
33+
}
34+
return result
35+
}

100.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
//给定两个二叉树,编写一个函数来检验它们是否相同。
4+
//
5+
//如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
6+
//
7+
//示例 1:
8+
//
9+
//输入: 1 1
10+
// / \ / \
11+
// 2 3 2 3
12+
//
13+
// [1,2,3], [1,2,3]
14+
//
15+
//输出: true
16+
//示例 2:
17+
//
18+
//输入: 1 1
19+
// / \
20+
// 2 2
21+
//
22+
// [1,2], [1,null,2]
23+
//
24+
//输出: false
25+
//示例 3:
26+
//
27+
//输入: 1 1
28+
// / \ / \
29+
// 2 1 1 2
30+
//
31+
// [1,2,1], [1,1,2]
32+
//
33+
//输出: false
34+
//
35+
//来源:力扣(LeetCode)
36+
//链接:https://leetcode-cn.com/problems/same-tree
37+
//著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
38+
39+
//func isSameTree(p *TreeNode, q *TreeNode) bool {
40+
// if p == nil && q == nil {
41+
// return true
42+
// }
43+
// return p != nil && q != nil &&
44+
// p.Val == q.Val &&
45+
// isSameTree(p.Left, q.Left) &&
46+
// isSameTree(p.Right, q.Right)
47+
//}

101.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
//给定一个二叉树,检查它是否是镜像对称的。
4+
//
5+
//
6+
//
7+
//例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
8+
//
9+
// 1
10+
// / \
11+
// 2 2
12+
// / \ / \
13+
//3 4 4 3
14+
//
15+
//
16+
//但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
17+
//
18+
// 1
19+
// / \
20+
// 2 2
21+
// \ \
22+
// 3 3
23+
//
24+
//symmetric-tree
25+
26+
type TreeNode struct {
27+
Val int
28+
Left *TreeNode
29+
Right *TreeNode
30+
}
31+
32+
func isMirror(r1, r2 *TreeNode) bool {
33+
if r1 == nil && r2 == nil {
34+
return true
35+
}
36+
if r1 == nil || r2 == nil {
37+
return false
38+
}
39+
if r1.Val != r2.Val {
40+
return false
41+
}
42+
return isMirror(r1.Left, r2.Right) && isMirror(r1.Right, r2.Left)
43+
}
44+
45+
func isSymmetric(root *TreeNode) bool {
46+
return isMirror(root, root)
47+
}

104.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"github.com/eapache/queue"
5+
)
6+
7+
8+
func main() {
9+
10+
}
11+
12+
//给定一个二叉树,找出其最大深度。
13+
//
14+
//二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
15+
//maximum-depth-of-binary-tree
16+
func maxDepth(root *TreeNode) int {
17+
/*
18+
递归
19+
*/
20+
//if root == nil {
21+
// return 0
22+
//} else {
23+
// return int(math.Max(float64(maxDepth(root.Left)), float64(maxDepth(root.Right)))) + 1
24+
//}
25+
26+
/*
27+
非递归
28+
*/
29+
if root == nil {
30+
return 0
31+
}
32+
result := 0
33+
queue1 := queue.New()
34+
queue1.Add(root)
35+
for queue1.Length() != 0 {
36+
result++
37+
queue2 := queue.New()
38+
for queue1.Length() != 0 {
39+
node := queue1.Remove().(*TreeNode)
40+
if node.Left != nil {
41+
queue2.Add(node.Left)
42+
}
43+
if node.Right != nil {
44+
queue2.Add(node.Right)
45+
}
46+
}
47+
queue1 = queue2
48+
}
49+
return result
50+
}

1042.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
4+
func main() {
5+
gardenNoAdj(10,nil)
6+
}
7+
8+
//有 N 个花园,按从 1 到 N 标记。在每个花园中,你打算种下四种花之一。
9+
//
10+
//paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径。
11+
//
12+
//另外,没有花园有 3 条以上的路径可以进入或者离开。
13+
//
14+
//你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同。
15+
//
16+
//以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类。花的种类用 1, 2, 3, 4 表示。保证存在答案。
17+
//
18+
//
19+
//
20+
//示例 1:
21+
//
22+
//输入:N = 3, paths = [[1,2],[2,3],[3,1]]
23+
//输出:[1,2,3]
24+
//示例 2:
25+
//
26+
//输入:N = 4, paths = [[1,2],[3,4]]
27+
//输出:[1,2,1,2]
28+
//示例 3:
29+
//
30+
//输入:N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
31+
//输出:[1,2,3,4]
32+
//flower-planting-with-no-adjacent
33+
func gardenNoAdj(N int, paths [][]int) []int {
34+
35+
}

108.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
//将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
4+
//
5+
//本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
6+
//
7+
//示例:
8+
//
9+
//给定有序数组: [-10,-3,0,5,9],
10+
//
11+
//一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:
12+
//
13+
// 0
14+
// / \
15+
// -3 9
16+
// / /
17+
// -10 5
18+
//
19+
//来源:力扣(LeetCode)
20+
//链接:https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
21+
//著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22+
23+
/**
24+
* Definition for a binary tree node.
25+
* type TreeNode struct {
26+
* Val int
27+
* Left *TreeNode
28+
* Right *TreeNode
29+
* }
30+
*/
31+
func sortedArrayToBST(nums []int) *TreeNode {
32+
if len(nums) == 0 {
33+
return nil
34+
}
35+
mid := len(nums) / 2
36+
left, right := nums[:mid], nums[mid+1:]
37+
node := &TreeNode{
38+
Val: nums[mid],
39+
Left: sortedArrayToBST(left),
40+
Right: sortedArrayToBST(right),
41+
}
42+
return node
43+
}

11.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
fmt.Println(maxArea([]int{1,8,6,2,5,4,8,3,7}))
10+
}
11+
12+
//给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
13+
//
14+
//说明:你不能倾斜容器,且 n 的值至少为 2。
15+
//示例:
16+
//
17+
//输入: [1,8,6,2,5,4,8,3,7]
18+
//输出: 49
19+
//container-with-most-water
20+
func maxArea(height []int) int {
21+
res, l, r := 0, 0, len(height)-1
22+
for l < r {
23+
res = int(math.Max(float64(res), math.Min(float64(height[l]), float64(height[r]))*float64(r-l)))
24+
if height[l] < height[r] {
25+
l++
26+
} else {
27+
r--
28+
}
29+
}
30+
return res
31+
}

1111.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println(maxDepthAfterSplit("()(())()"))
7+
}
8+
9+
//有效括号字符串 定义:对于每个左括号,都能找到与之对应的右括号,反之亦然。详情参见题末「有效括号字符串」部分。
10+
//
11+
//嵌套深度 depth 定义:即有效括号字符串嵌套的层数,depth(A) 表示有效括号字符串 A 的嵌套深度。详情参见题末「嵌套深度」部分。
12+
//
13+
//有效括号字符串类型与对应的嵌套深度计算方法如下图所示:
14+
//
15+
//
16+
//给你一个「有效括号字符串」 seq,请你将其分成两个不相交的有效括号字符串,A 和 B,并使这两个字符串的深度最小。
17+
//
18+
//不相交:每个 seq[i] 只能分给 A 和 B 二者中的一个,不能既属于 A 也属于 B 。
19+
//A 或 B 中的元素在原字符串中可以不连续。
20+
//A.length + B.length = seq.length
21+
//深度最小:max(depth(A), depth(B)) 的可能取值最小。
22+
//划分方案用一个长度为 seq.length 的答案数组 answer 表示,编码规则如下:
23+
//
24+
//answer[i] = 0,seq[i] 分给 A 。
25+
//answer[i] = 1,seq[i] 分给 B 。
26+
//如果存在多个满足要求的答案,只需返回其中任意 一个 即可。
27+
//
28+
//
29+
//
30+
//示例 1:
31+
//
32+
//输入:seq = "(()())"
33+
//输出:[0,1,1,1,1,0]
34+
//示例 2:
35+
//
36+
//输入:seq = "()(())()"
37+
//输出:[0,0,0,1,1,0,1,1]
38+
//解释:本示例答案不唯一。
39+
//按此输出 A = "()()", B = "()()", max(depth(A), depth(B)) = 1 。
40+
//像 [1,1,1,0,0,1,1,1],也是正确结果,其中 A = "()()()", B = "()", max(depth(A), depth(B)) = 1 。
41+
//
42+
//
43+
//提示:
44+
//
45+
//1 <= text.size <= 10000
46+
//
47+
//
48+
//有效括号字符串:
49+
//
50+
//仅由 "(" 和 ")" 构成的字符串,对于每个左括号,都能找到与之对应的右括号,反之亦然。
51+
//下述几种情况同样属于有效括号字符串:
52+
//
53+
// 1. 空字符串
54+
// 2. 连接,可以记作 AB(A 与 B 连接),其中 A 和 B 都是有效括号字符串
55+
// 3. 嵌套,可以记作 (A),其中 A 是有效括号字符串
56+
//嵌套深度:
57+
//
58+
//类似地,我们可以定义任意有效括号字符串 s 的 嵌套深度 depth(S):
59+
//
60+
// 1. s 为空时,depth("") = 0
61+
// 2. s 为 A 与 B 连接时,depth(A + B) = max(depth(A), depth(B)),其中 A 和 B 都是有效括号字符串
62+
// 3. s 为嵌套情况,depth("(" + A + ")") = 1 + depth(A),其中 A 是有效括号字符串
63+
//
64+
//例如:"","()()",和 "()(()())" 都是有效括号字符串,嵌套深度分别为 0,1,2,而 ")(" 和 "(()" 都不是有效括号字符串。
65+
//maximum-nesting-depth-of-two-valid-parentheses-strings
66+
67+
func maxDepthAfterSplit(seq string) []int {
68+
// 奇偶性直接判断 证明见leetcode
69+
res := make([]int, len(seq))
70+
for i, s := range seq {
71+
if s == '(' {
72+
res[i] = i & 1
73+
} else {
74+
res[i] = (i + 1) & 1
75+
}
76+
}
77+
return res
78+
}

0 commit comments

Comments
 (0)