Skip to content

Commit 22ef8d8

Browse files
committed
update
1 parent 57ebca2 commit 22ef8d8

File tree

88 files changed

+3220
-2565
lines changed

Some content is hidden

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

88 files changed

+3220
-2565
lines changed

1.两数之和.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/*
2-
* @lc app=leetcode.cn id=1 lang=golang
3-
*
4-
* [1] 两数之和
5-
*/
6-
7-
// @lc code=start
8-
9-
func twoSum(nums []int, target int) []int {
10-
cache := make(map[int]int)
11-
for i := 0; i < len(nums); i++ {
12-
current := nums[i]
13-
subtract := target - current
14-
if v, ok := cache[subtract]; ok {
15-
return []int{v, i}
16-
}
17-
cache[current] = i
18-
}
19-
return []int{}
20-
}
21-
22-
// @lc code=end
1+
/*
2+
* @lc app=leetcode.cn id=1 lang=golang
3+
*
4+
* [1] 两数之和
5+
*/
6+
7+
// @lc code=start
8+
9+
func twoSum(nums []int, target int) []int {
10+
cache := make(map[int]int)
11+
for i := 0; i < len(nums); i++ {
12+
current := nums[i]
13+
subtract := target - current
14+
if v, ok := cache[subtract]; ok {
15+
return []int{v, i}
16+
}
17+
cache[current] = i
18+
}
19+
return []int{}
20+
}
21+
22+
// @lc code=end

100.相同的树.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
/*
2-
* @lc app=leetcode.cn id=100 lang=golang
3-
*
4-
* [100] 相同的树
5-
*/
6-
7-
// @lc code=start
8-
/**
9-
* Definition for a binary tree node.
10-
* type TreeNode struct {
11-
* Val int
12-
* Left *TreeNode
13-
* Right *TreeNode
14-
* }
15-
*/
16-
func isSameTree(p *TreeNode, q *TreeNode) bool {
17-
if p == nil && q == nil {
18-
return true
19-
}
20-
if p == nil || q == nil {
21-
return false
22-
}
23-
if p.Val != q.Val {
24-
return false
25-
}
26-
27-
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
28-
29-
}
30-
31-
// @lc code=end
32-
1+
/*
2+
* @lc app=leetcode.cn id=100 lang=golang
3+
*
4+
* [100] 相同的树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func isSameTree(p *TreeNode, q *TreeNode) bool {
17+
if p == nil && q == nil {
18+
return true
19+
}
20+
if p == nil || q == nil {
21+
return false
22+
}
23+
if p.Val != q.Val {
24+
return false
25+
}
26+
27+
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
28+
29+
}
30+
31+
// @lc code=end
32+

101.对称二叉树.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
/*
2-
* @lc app=leetcode.cn id=101 lang=golang
3-
*
4-
* [101] 对称二叉树
5-
*/
6-
7-
// @lc code=start
8-
/**
9-
* Definition for a binary tree node.
10-
* type TreeNode struct {
11-
* Val int
12-
* Left *TreeNode
13-
* Right *TreeNode
14-
* }
15-
*/
16-
func isSymmetric(root *TreeNode) bool {
17-
if root == nil {
18-
return false
19-
}
20-
21-
return check(root, root)
22-
}
23-
24-
func check(node1, node2 *TreeNode) bool {
25-
if node1 == nil && node2 == nil {
26-
return true
27-
}
28-
if node1 == nil || node2 == nil {
29-
return false
30-
}
31-
return node1.Val == node2.Val && check(node1.Left, node2.Right) && check(node1.Right, node2.Left)
32-
}
33-
34-
// @lc code=end
35-
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=golang
3+
*
4+
* [101] 对称二叉树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func isSymmetric(root *TreeNode) bool {
17+
if root == nil {
18+
return false
19+
}
20+
21+
return check(root, root)
22+
}
23+
24+
func check(node1, node2 *TreeNode) bool {
25+
if node1 == nil && node2 == nil {
26+
return true
27+
}
28+
if node1 == nil || node2 == nil {
29+
return false
30+
}
31+
return node1.Val == node2.Val && check(node1.Left, node2.Right) && check(node1.Right, node2.Left)
32+
}
33+
34+
// @lc code=end
35+

104.二叉树的最大深度.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
/*
2-
* @lc app=leetcode.cn id=104 lang=golang
3-
*
4-
* [104] 二叉树的最大深度
5-
*/
6-
7-
// @lc code=start
8-
/**
9-
* Definition for a binary tree node.
10-
* type TreeNode struct {
11-
* Val int
12-
* Left *TreeNode
13-
* Right *TreeNode
14-
* }
15-
*/
16-
func maxDepth(root *TreeNode) int {
17-
if root == nil {
18-
return 0
19-
}
20-
21-
if root.Left == nil && root.Right == nil {
22-
return 1
23-
}
24-
return 1 + max(maxDepth(root.Left), maxDepth(root.Right))
25-
}
26-
27-
func max(x, y int) int {
28-
if x > y {
29-
return x
30-
}
31-
return y
32-
}
33-
34-
// @lc code=end
35-
1+
/*
2+
* @lc app=leetcode.cn id=104 lang=golang
3+
*
4+
* [104] 二叉树的最大深度
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func maxDepth(root *TreeNode) int {
17+
if root == nil {
18+
return 0
19+
}
20+
21+
if root.Left == nil && root.Right == nil {
22+
return 1
23+
}
24+
return 1 + max(maxDepth(root.Left), maxDepth(root.Right))
25+
}
26+
27+
func max(x, y int) int {
28+
if x > y {
29+
return x
30+
}
31+
return y
32+
}
33+
34+
// @lc code=end
35+
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
/*
2-
* @lc app=leetcode.cn id=108 lang=golang
3-
*
4-
* [108] 将有序数组转换为二叉搜索树
5-
*/
6-
7-
// @lc code=start
8-
/**
9-
* Definition for a binary tree node.
10-
* type TreeNode struct {
11-
* Val int
12-
* Left *TreeNode
13-
* Right *TreeNode
14-
* }
15-
*/
16-
func sortedArrayToBST(nums []int) *TreeNode {
17-
/*
18-
bst的inorder结果即为递增数组
19-
因此每次选择下列节点为根节点均可
20-
1. 中间左边
21-
2. 中间右边
22-
3. 中间
23-
*/
24-
25-
return bst(nums, 0, len(nums)-1)
26-
}
27-
28-
func bst(nums []int, left, right int) *TreeNode {
29-
if left > right {
30-
return nil
31-
}
32-
root := new(TreeNode)
33-
34-
// left <= right
35-
mid := (left + right) / 2
36-
root.Val = nums[mid]
37-
root.Left = bst(nums, left, mid-1)
38-
root.Right = bst(nums, mid+1, right)
39-
return root
40-
}
41-
42-
// @lc code=end
43-
1+
/*
2+
* @lc app=leetcode.cn id=108 lang=golang
3+
*
4+
* [108] 将有序数组转换为二叉搜索树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func sortedArrayToBST(nums []int) *TreeNode {
17+
/*
18+
bst的inorder结果即为递增数组
19+
因此每次选择下列节点为根节点均可
20+
1. 中间左边
21+
2. 中间右边
22+
3. 中间
23+
*/
24+
25+
return bst(nums, 0, len(nums)-1)
26+
}
27+
28+
func bst(nums []int, left, right int) *TreeNode {
29+
if left > right {
30+
return nil
31+
}
32+
root := new(TreeNode)
33+
34+
// left <= right
35+
mid := (left + right) / 2
36+
root.Val = nums[mid]
37+
root.Left = bst(nums, left, mid-1)
38+
root.Right = bst(nums, mid+1, right)
39+
return root
40+
}
41+
42+
// @lc code=end
43+

0 commit comments

Comments
 (0)