mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 14:41:34 +08:00
添加 problem 223
This commit is contained in:
14
Algorithms/0223. Rectangle Area/223. Rectangle Area.go
Normal file
14
Algorithms/0223. Rectangle Area/223. Rectangle Area.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int {
|
||||||
|
X0, Y0, X1, Y1 := max(A, E), max(B, F), min(C, G), min(D, H)
|
||||||
|
return area(A, B, C, D) + area(E, F, G, H) - area(X0, Y0, X1, Y1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func area(x0, y0, x1, y1 int) int {
|
||||||
|
l, h := x1-x0, y1-y0
|
||||||
|
if l <= 0 || h <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return l * h
|
||||||
|
}
|
49
Algorithms/0223. Rectangle Area/223. Rectangle Area_test.go
Normal file
49
Algorithms/0223. Rectangle Area/223. Rectangle Area_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question223 struct {
|
||||||
|
para223
|
||||||
|
ans223
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para223 struct {
|
||||||
|
A int
|
||||||
|
B int
|
||||||
|
C int
|
||||||
|
D int
|
||||||
|
E int
|
||||||
|
F int
|
||||||
|
G int
|
||||||
|
H int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans223 struct {
|
||||||
|
one int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem223(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question223{
|
||||||
|
|
||||||
|
question223{
|
||||||
|
para223{-3, 0, 3, 4, 0, -1, 9, 2},
|
||||||
|
ans223{45},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 223------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans223, q.para223
|
||||||
|
fmt.Printf("【input】:%v 【output】:%v\n", p, computeArea(p.A, p.B, p.C, p.D, p.E, p.F, p.G, p.H))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
31
Algorithms/0223. Rectangle Area/README.md
Executable file
31
Algorithms/0223. Rectangle Area/README.md
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
# [223. Rectangle Area](https://leetcode.com/problems/rectangle-area/)
|
||||||
|
|
||||||
|
|
||||||
|
## 题目:
|
||||||
|
|
||||||
|
Find the total area covered by two **rectilinear** rectangles in a **2D** plane.
|
||||||
|
|
||||||
|
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
|
||||||
|
Output: 45
|
||||||
|
|
||||||
|
**Note:**
|
||||||
|
|
||||||
|
Assume that the total area is never beyond the maximum possible value of **int**.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 题目大意
|
||||||
|
|
||||||
|
在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。每个矩形由其左下顶点和右上顶点坐标表示,如图所示。说明: 假设矩形面积不会超出 int 的范围。
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
|
||||||
|
- 给出两个矩形的坐标,求这两个矩形在坐标轴上覆盖的总面积。
|
||||||
|
- 几何题,由于只有 2 个矩形,所以按照题意做即可。先分别求两个矩形的面积,加起来再减去两个矩形重叠的面积。
|
Reference in New Issue
Block a user