-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,926 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright (C) 2022 lazywhite <[email protected]> | ||
// | ||
// Distributed under terms of the MIT license. | ||
// | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
//fmt.Println(getCommonDivisor(28, 14)) | ||
fmt.Println(getCommonMultiple(25, 10)) | ||
} | ||
|
||
// 最大公约数 | ||
// 更相减损术 | ||
func getCommonDivisor(a, b int) int { | ||
big := a | ||
small := b | ||
if a < b { | ||
big = b | ||
small = a | ||
} | ||
|
||
if big%small == 0 { | ||
return small | ||
} | ||
return getCommonDivisor(big-small, small) | ||
} | ||
|
||
// 最大公倍数 | ||
// 两数的乘积除以最大公约数 | ||
func getCommonMultiple(a, b int) int{ | ||
|
||
multi := a * b | ||
divisor := getCommonDivisor(a, b) | ||
|
||
// 一定能整除 | ||
return multi/divisor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Copyright (C) 2022 lazywhite <[email protected]> | ||
// | ||
// Distributed under terms of the MIT license. | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main(){ | ||
|
||
a := 10 | ||
b := 20 | ||
/* | ||
a = 10 ^ 20 | ||
b = 10 ^ 20 ^ 20 = 10 | ||
a = 10 ^ 20 ^ 10 = 20 | ||
*/ | ||
|
||
a = a ^ b | ||
b = a ^ b | ||
a = a ^ b | ||
fmt.Println(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* @lc app=leetcode.cn id=116 lang=golang | ||
* | ||
* [116] 填充每个节点的下一个右侧节点指针 | ||
* | ||
* https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/description/ | ||
* | ||
* algorithms | ||
* Medium (71.21%) | ||
* Likes: 741 | ||
* Dislikes: 0 | ||
* Total Accepted: 235.9K | ||
* Total Submissions: 330.4K | ||
* Testcase Example: '[1,2,3,4,5,6,7]' | ||
* | ||
* 给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下: | ||
* | ||
* | ||
* struct Node { | ||
* int val; | ||
* Node *left; | ||
* Node *right; | ||
* Node *next; | ||
* } | ||
* | ||
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。 | ||
* | ||
* 初始状态下,所有 next 指针都被设置为 NULL。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* | ||
* | ||
* | ||
* 输入:root = [1,2,3,4,5,6,7] | ||
* 输出:[1,#,2,3,#,4,5,6,7,#] | ||
* 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B | ||
* 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。 | ||
* | ||
* | ||
* | ||
* | ||
* 示例 2: | ||
* | ||
* | ||
* 输入:root = [] | ||
* 输出:[] | ||
* | ||
* | ||
* | ||
* | ||
* 提示: | ||
* | ||
* | ||
* 树中节点的数量在 [0, 2^12 - 1] 范围内 | ||
* -1000 <= node.val <= 1000 | ||
* | ||
* | ||
* | ||
* | ||
* 进阶: | ||
* | ||
* | ||
* 你只能使用常量级额外空间。 | ||
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。 | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a Node. | ||
* type Node struct { | ||
* Val int | ||
* Left *Node | ||
* Right *Node | ||
* Next *Node | ||
* } | ||
*/ | ||
|
||
func connect(root *Node) *Node { | ||
|
||
if root.Left == nil { | ||
return root | ||
} | ||
|
||
} | ||
|
||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* @lc app=leetcode.cn id=120 lang=golang | ||
* | ||
* [120] 三角形最小路径和 | ||
* | ||
* https://leetcode-cn.com/problems/triangle/description/ | ||
* | ||
* algorithms | ||
* Medium (68.47%) | ||
* Likes: 1012 | ||
* Dislikes: 0 | ||
* Total Accepted: 225.7K | ||
* Total Submissions: 329.3K | ||
* Testcase Example: '[[2],[3,4],[6,5,7],[4,1,8,3]]' | ||
* | ||
* 给定一个三角形 triangle ,找出自顶向下的最小路径和。 | ||
* | ||
* 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 | ||
* 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* | ||
* 输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] | ||
* 输出:11 | ||
* 解释:如下面简图所示: | ||
* 2 | ||
* 3 4 | ||
* 6 5 7 | ||
* 4 1 8 3 | ||
* 自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。 | ||
* | ||
* | ||
* 示例 2: | ||
* | ||
* | ||
* 输入:triangle = [[-10]] | ||
* 输出:-10 | ||
* | ||
* | ||
* | ||
* | ||
* 提示: | ||
* | ||
* | ||
* 1 | ||
* triangle[0].length == 1 | ||
* triangle[i].length == triangle[i - 1].length + 1 | ||
* -10^4 | ||
* | ||
* | ||
* | ||
* | ||
* 进阶: | ||
* | ||
* | ||
* 你可以只使用 O(n) 的额外空间(n 为三角形的总行数)来解决这个问题吗? | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
func minimumTotal(triangle [][]int) int { | ||
|
||
row := len(triangle) | ||
|
||
dp := make([][]int, row) | ||
for i := 0; i < row; i++ { | ||
dp[i] = make([]int, len(triangle[i])) | ||
} | ||
|
||
dp[0][0] = triangle[0][0] | ||
for i := 1; i < row; i++ { | ||
for j := 0; j < len(triangle[i]); j++ { | ||
if j == 0 { | ||
dp[i][0] = dp[i-1][0] + triangle[i][0] | ||
} else if j == len(triangle[i])-1 { | ||
dp[i][j] = dp[i-1][j-1] + triangle[i][j] | ||
} else { | ||
dp[i][j] = min(dp[i-1][j], dp[i-1][j-1]) + triangle[i][j] | ||
} | ||
} | ||
} | ||
|
||
return minNum(dp[row-1]) | ||
} | ||
|
||
func min(x, y int) int { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
func minNum(arr []int) int { | ||
ans := arr[0] | ||
|
||
for i := 1; i < len(arr); i++ { | ||
if arr[i] < ans { | ||
ans = arr[i] | ||
} | ||
} | ||
return ans | ||
} | ||
|
||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* @lc app=leetcode.cn id=124 lang=golang | ||
* | ||
* [124] 二叉树中的最大路径和 | ||
* | ||
* https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/ | ||
* | ||
* algorithms | ||
* Hard (44.83%) | ||
* Likes: 1532 | ||
* Dislikes: 0 | ||
* Total Accepted: 219.3K | ||
* Total Submissions: 488.2K | ||
* Testcase Example: '[1,2,3]' | ||
* | ||
* 路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 | ||
* 节点,且不一定经过根节点。 | ||
* | ||
* 路径和 是路径中各节点值的总和。 | ||
* | ||
* 给你一个二叉树的根节点 root ,返回其 最大路径和 。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* | ||
* 输入:root = [1,2,3] | ||
* 输出:6 | ||
* 解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6 | ||
* | ||
* 示例 2: | ||
* | ||
* | ||
* 输入:root = [-10,9,20,null,null,15,7] | ||
* 输出:42 | ||
* 解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42 | ||
* | ||
* | ||
* | ||
* | ||
* 提示: | ||
* | ||
* | ||
* 树中节点数目范围是 [1, 3 * 10^4] | ||
* -1000 | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func maxPathSum(root *TreeNode) int { | ||
|
||
maxSum := -1000 | ||
var dfs func(*TreeNode) int | ||
|
||
dfs = func(n *TreeNode) int { | ||
if n == nil { | ||
return 0 | ||
} | ||
left := max(0, dfs(n.Left)) | ||
right := max(0, dfs(n.Right)) | ||
|
||
maxSum = max(maxSum, n.Val+left+right) | ||
|
||
return n.Val + max(left, right) | ||
|
||
} | ||
|
||
dfs(root) | ||
return maxSum | ||
} | ||
|
||
func max(x, y int) int { | ||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// @lc code=end | ||
|
Oops, something went wrong.