-
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
38 changed files
with
3,302 additions
and
2,114 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,92 @@ | ||
/* | ||
* @lc app=leetcode.cn id=11 lang=golang | ||
* | ||
* [11] 盛最多水的容器 | ||
* | ||
* https://leetcode-cn.com/problems/container-with-most-water/description/ | ||
* | ||
* algorithms | ||
* Medium (61.73%) | ||
* Likes: 3413 | ||
* Dislikes: 0 | ||
* Total Accepted: 701.5K | ||
* Total Submissions: 1.1M | ||
* Testcase Example: '[1,8,6,2,5,4,8,3,7]' | ||
* | ||
* 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 | ||
* | ||
* 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 | ||
* | ||
* 返回容器可以储存的最大水量。 | ||
* | ||
* 说明:你不能倾斜容器。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* | ||
* | ||
* | ||
* 输入:[1,8,6,2,5,4,8,3,7] | ||
* 输出:49 | ||
* 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 | ||
* | ||
* 示例 2: | ||
* | ||
* | ||
* 输入:height = [1,1] | ||
* 输出:1 | ||
* | ||
* | ||
* | ||
* | ||
* 提示: | ||
* | ||
* | ||
* n == height.length | ||
* 2 <= n <= 10^5 | ||
* 0 <= height[i] <= 10^4 | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
func maxArea(height []int) int { | ||
last := len(height) - 1 | ||
first := 0 | ||
cap := 0 | ||
if last <= 0 { | ||
return 0 | ||
} | ||
for first < last { | ||
width := last - first | ||
height := min(height[first], height[last]) | ||
|
||
cap = max(cap, width*height) | ||
|
||
if height[first] < height[last] { | ||
first++ | ||
} else { | ||
last-- | ||
} | ||
} | ||
return cap | ||
|
||
} | ||
|
||
func max(x, y int) int { | ||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
func min(x, y int) int { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// @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 |
---|---|---|
@@ -1,92 +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 | ||
/* | ||
* @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 | ||
|
Oops, something went wrong.