Skip to content

Commit deafba0

Browse files
committed
update
1 parent e246e20 commit deafba0

25 files changed

+1926
-8
lines changed

002.公约数公倍数.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Copyright (C) 2022 lazywhite <[email protected]>
3+
//
4+
// Distributed under terms of the MIT license.
5+
//
6+
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
13+
//fmt.Println(getCommonDivisor(28, 14))
14+
fmt.Println(getCommonMultiple(25, 10))
15+
}
16+
17+
// 最大公约数
18+
// 更相减损术
19+
func getCommonDivisor(a, b int) int {
20+
big := a
21+
small := b
22+
if a < b {
23+
big = b
24+
small = a
25+
}
26+
27+
if big%small == 0 {
28+
return small
29+
}
30+
return getCommonDivisor(big-small, small)
31+
}
32+
33+
// 最大公倍数
34+
// 两数的乘积除以最大公约数
35+
func getCommonMultiple(a, b int) int{
36+
37+
multi := a * b
38+
divisor := getCommonDivisor(a, b)
39+
40+
// 一定能整除
41+
return multi/divisor
42+
}

003.原地交换整数的值.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright (C) 2022 lazywhite <[email protected]>
3+
//
4+
// Distributed under terms of the MIT license.
5+
//
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
func main(){
14+
15+
a := 10
16+
b := 20
17+
/*
18+
a = 10 ^ 20
19+
b = 10 ^ 20 ^ 20 = 10
20+
a = 10 ^ 20 ^ 10 = 20
21+
*/
22+
23+
a = a ^ b
24+
b = a ^ b
25+
a = a ^ b
26+
fmt.Println(a, b)
27+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @lc app=leetcode.cn id=116 lang=golang
3+
*
4+
* [116] 填充每个节点的下一个右侧节点指针
5+
*
6+
* https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/description/
7+
*
8+
* algorithms
9+
* Medium (71.21%)
10+
* Likes: 741
11+
* Dislikes: 0
12+
* Total Accepted: 235.9K
13+
* Total Submissions: 330.4K
14+
* Testcase Example: '[1,2,3,4,5,6,7]'
15+
*
16+
* 给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
17+
*
18+
*
19+
* struct Node {
20+
* ⁠ int val;
21+
* ⁠ Node *left;
22+
* ⁠ Node *right;
23+
* ⁠ Node *next;
24+
* }
25+
*
26+
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
27+
*
28+
* 初始状态下,所有 next 指针都被设置为 NULL。
29+
*
30+
*
31+
*
32+
* 示例 1:
33+
*
34+
*
35+
*
36+
*
37+
* 输入:root = [1,2,3,4,5,6,7]
38+
* 输出:[1,#,2,3,#,4,5,6,7,#]
39+
* 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B
40+
* 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
41+
*
42+
*
43+
*
44+
*
45+
* 示例 2:
46+
*
47+
*
48+
* 输入:root = []
49+
* 输出:[]
50+
*
51+
*
52+
*
53+
*
54+
* 提示:
55+
*
56+
*
57+
* 树中节点的数量在 [0, 2^12 - 1] 范围内
58+
* -1000 <= node.val <= 1000
59+
*
60+
*
61+
*
62+
*
63+
* 进阶:
64+
*
65+
*
66+
* 你只能使用常量级额外空间。
67+
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
68+
*
69+
*
70+
*/
71+
72+
// @lc code=start
73+
/**
74+
* Definition for a Node.
75+
* type Node struct {
76+
* Val int
77+
* Left *Node
78+
* Right *Node
79+
* Next *Node
80+
* }
81+
*/
82+
83+
func connect(root *Node) *Node {
84+
85+
if root.Left == nil {
86+
return root
87+
}
88+
89+
}
90+
91+
// @lc code=end
92+

120.三角形最小路径和.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* @lc app=leetcode.cn id=120 lang=golang
3+
*
4+
* [120] 三角形最小路径和
5+
*
6+
* https://leetcode-cn.com/problems/triangle/description/
7+
*
8+
* algorithms
9+
* Medium (68.47%)
10+
* Likes: 1012
11+
* Dislikes: 0
12+
* Total Accepted: 225.7K
13+
* Total Submissions: 329.3K
14+
* Testcase Example: '[[2],[3,4],[6,5,7],[4,1,8,3]]'
15+
*
16+
* 给定一个三角形 triangle ,找出自顶向下的最小路径和。
17+
*
18+
* 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1
19+
* 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。
20+
*
21+
*
22+
*
23+
* 示例 1:
24+
*
25+
*
26+
* 输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
27+
* 输出:11
28+
* 解释:如下面简图所示:
29+
* ⁠ 2
30+
* ⁠ 3 4
31+
* ⁠6 5 7
32+
* 4 1 8 3
33+
* 自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
*
39+
* 输入:triangle = [[-10]]
40+
* 输出:-10
41+
*
42+
*
43+
*
44+
*
45+
* 提示:
46+
*
47+
*
48+
* 1
49+
* triangle[0].length == 1
50+
* triangle[i].length == triangle[i - 1].length + 1
51+
* -10^4
52+
*
53+
*
54+
*
55+
*
56+
* 进阶:
57+
*
58+
*
59+
* 你可以只使用 O(n) 的额外空间(n 为三角形的总行数)来解决这个问题吗?
60+
*
61+
*
62+
*/
63+
64+
// @lc code=start
65+
func minimumTotal(triangle [][]int) int {
66+
67+
row := len(triangle)
68+
69+
dp := make([][]int, row)
70+
for i := 0; i < row; i++ {
71+
dp[i] = make([]int, len(triangle[i]))
72+
}
73+
74+
dp[0][0] = triangle[0][0]
75+
for i := 1; i < row; i++ {
76+
for j := 0; j < len(triangle[i]); j++ {
77+
if j == 0 {
78+
dp[i][0] = dp[i-1][0] + triangle[i][0]
79+
} else if j == len(triangle[i])-1 {
80+
dp[i][j] = dp[i-1][j-1] + triangle[i][j]
81+
} else {
82+
dp[i][j] = min(dp[i-1][j], dp[i-1][j-1]) + triangle[i][j]
83+
}
84+
}
85+
}
86+
87+
return minNum(dp[row-1])
88+
}
89+
90+
func min(x, y int) int {
91+
if x < y {
92+
return x
93+
}
94+
return y
95+
}
96+
97+
func minNum(arr []int) int {
98+
ans := arr[0]
99+
100+
for i := 1; i < len(arr); i++ {
101+
if arr[i] < ans {
102+
ans = arr[i]
103+
}
104+
}
105+
return ans
106+
}
107+
108+
// @lc code=end
109+

124.二叉树中的最大路径和.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* @lc app=leetcode.cn id=124 lang=golang
3+
*
4+
* [124] 二叉树中的最大路径和
5+
*
6+
* https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/
7+
*
8+
* algorithms
9+
* Hard (44.83%)
10+
* Likes: 1532
11+
* Dislikes: 0
12+
* Total Accepted: 219.3K
13+
* Total Submissions: 488.2K
14+
* Testcase Example: '[1,2,3]'
15+
*
16+
* 路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个
17+
* 节点,且不一定经过根节点。
18+
*
19+
* 路径和 是路径中各节点值的总和。
20+
*
21+
* 给你一个二叉树的根节点 root ,返回其 最大路径和 。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:root = [1,2,3]
29+
* 输出:6
30+
* 解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:root = [-10,9,20,null,null,15,7]
36+
* 输出:42
37+
* 解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
38+
*
39+
*
40+
*
41+
*
42+
* 提示:
43+
*
44+
*
45+
* 树中节点数目范围是 [1, 3 * 10^4]
46+
* -1000
47+
*
48+
*
49+
*/
50+
51+
// @lc code=start
52+
/**
53+
* Definition for a binary tree node.
54+
* type TreeNode struct {
55+
* Val int
56+
* Left *TreeNode
57+
* Right *TreeNode
58+
* }
59+
*/
60+
func maxPathSum(root *TreeNode) int {
61+
62+
maxSum := -1000
63+
var dfs func(*TreeNode) int
64+
65+
dfs = func(n *TreeNode) int {
66+
if n == nil {
67+
return 0
68+
}
69+
left := max(0, dfs(n.Left))
70+
right := max(0, dfs(n.Right))
71+
72+
maxSum = max(maxSum, n.Val+left+right)
73+
74+
return n.Val + max(left, right)
75+
76+
}
77+
78+
dfs(root)
79+
return maxSum
80+
}
81+
82+
func max(x, y int) int {
83+
if x > y {
84+
return x
85+
}
86+
return y
87+
}
88+
89+
// @lc code=end
90+

0 commit comments

Comments
 (0)