目录
223. 矩形面积 Rectangle Area 🌟🌟
226. 翻转二叉树 Invert Binary Tree 🌟
🌟 每日一练刷题专栏 🌟
Rust每日一练 专栏
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
223. 矩形面积 Rectangle Area
给你 二维 平面上两个 由直线构成且边与坐标轴平行/垂直 的矩形,请你计算并返回两个矩形覆盖的总面积。
每个矩形由其 左下 顶点和 右上 顶点坐标表示:
- 第一个矩形由其左下顶点
(ax1, ay1)
和右上顶点(ax2, ay2)
定义。 - 第二个矩形由其左下顶点
(bx1, by1)
和右上顶点(bx2, by2)
定义。
示例 1:
输入:ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 输出:45
示例 2:
输入:ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2 输出:16
提示:
-10^4 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 10^4
代码:
package main
import (
"fmt"
)
func computeArea(ax1 int, ay1 int, ax2 int, ay2 int, bx1 int, by1 int, bx2 int, by2 int) int {
area1 := (ax2 - ax1) * (ay2 - ay1)
area2 := (bx2 - bx1) * (by2 - by1)
overlapWidth, overlapHeight := 0, 0
overlapWidth = min(ax2, bx2) - max(ax1, bx1)
overlapHeight = min(ay2, by2) - max(ay1, by1)
overlapArea := max(overlapWidth, 0) * max(overlapHeight, 0)
return area1 + area2 - overlapArea
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func main() {
ax1 := -3; ay1 := 0; ax2 := 3; ay2 := 4
bx1 := 0; by1 := -1; bx2 := 9; by2 := 2
fmt.Println(computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2))
ax1 = -2; ay1 = -2; ax2 = 2; ay2 = 2
bx1 = -2; by1 = -2; bx2 = 2; by2 = 2
fmt.Println(computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2))
}
输出:
45
16
226. 翻转二叉树 Invert Binary Tree
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
示例 1:
输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1]
示例 2:
输入:root = [2,1,3] 输出:[2,3,1]
示例 3:
输入:root = [] 输出:[]
提示:
- 树中节点数目范围在
[0, 100]
内 -100 <= Node.val <= 100
代码:
package main
import "fmt"
const null = -1 << 31
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
queue := []*TreeNode{root}
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
left := curr.Left
curr.Left = curr.Right
curr.Right = left
if curr.Left != nil {
queue = append(queue, curr.Left)
}
if curr.Right != nil {
queue = append(queue, curr.Right)
}
}
return root
}
func buildTree(nums []int) *TreeNode {
if len(nums) == 0 {
return nil
}
root := &TreeNode{Val: nums[0]}
Queue := []*TreeNode{root}
idx := 1
for idx < len(nums) {
node := Queue[0]
Queue = Queue[1:]
if nums[idx] != null {
node.Left = &TreeNode{Val: nums[idx]}
Queue = append(Queue, node.Left)
}
idx++
if idx < len(nums) && nums[idx] != null {
node.Right = &TreeNode{Val: nums[idx]}
Queue = append(Queue, node.Right)
}
idx++
}
return root
}
func levelOrder(root *TreeNode) []int {
res := []int{}
if root == nil {
return res
}
Queue := []*TreeNode{root}
for len(Queue) > 0 {
level := []int{}
n := len(Queue)
for i := 0; i < n; i++ {
cur := Queue[0]
Queue = Queue[1:]
level = append(level, cur.Val)
if cur.Left != nil {
Queue = append(Queue, cur.Left)
}
if cur.Right != nil {
Queue = append(Queue, cur.Right)
}
}
res = append(res, level...)
}
return res
}
func ArrayToString(arr []int) string {
res := "["
for i := 0; i < len(arr); i++ {
res += fmt.Sprint(arr[i]) //对[]int数组可以用strconv.Itoa(arr[i])
if i != len(arr)-1 {
res += ","
}
}
return res + "]"
}
func main() {
nums := []int{4, 2, 7, 1, 3, 6, 9}
root := buildTree(nums)
fmt.Println(ArrayToString(levelOrder(root)))
root = invertTree(root)
fmt.Println(ArrayToString(levelOrder(root)))
nums = []int{2, 1, 3}
root = buildTree(nums)
fmt.Println(ArrayToString(levelOrder(root)))
root = invertTree(root)
fmt.Println(ArrayToString(levelOrder(root)))
}
输出:
[4,2,7,1,3,6,9]
[4,7,2,9,6,3,1]
[2,1,3]
[2,3,1]
递归法:
```Go
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
left := invertTree(root.Left)
right := invertTree(root.Right)
root.Left = right
root.Right = left
return root
}
```
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Rust每日一练 专栏(2023.5.16~)更新中... | |
Golang每日一练 专栏(2023.3.11~)更新中... | |
Python每日一练 专栏(2023.2.18~2023.5.18)暂停更 | |
C/C++每日一练 专栏(2023.2.18~2023.5.18)暂停更 | |
Java每日一练 专栏(2023.3.11~2023.5.18)暂停更 |