FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0223_RectangleArea.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0223_RectangleArea.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (44 loc) · 1.76 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0223_RectangleArea.java
Copy path
File metadata and controls
51 lines (44 loc) · 1.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* https://leetcode.cn/problems/rectangle-area/
*
* 在二维平面上实现一个函数,计算其中两个由直线构成的矩形重叠后形成的总面积。
*
* 每个矩形由其左下顶点和右上顶点坐标表示。
*
* 示例 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
*/
public
class
L0223_RectangleArea
{
public
int
computeArea
(
int
ax1
,
int
ay1
,
int
ax2
,
int
ay2
,
int
bx1
,
int
by1
,
int
bx2
,
int
by2
) {
// 计算两个矩形的面积
int
area1
= (
ax2
-
ax1
) * (
ay2
-
ay1
);
int
area2
= (
bx2
-
bx1
) * (
by2
-
by1
);
// 计算重叠区域的坐标
int
overlapX1
=
Math
.
max
(
ax1
,
bx1
);
int
overlapY1
=
Math
.
max
(
ay1
,
by1
);
int
overlapX2
=
Math
.
min
(
ax2
,
bx2
);
int
overlapY2
=
Math
.
min
(
ay2
,
by2
);
// 计算重叠区域的面积
int
overlapArea
=
0
;
if
(
overlapX2
>
overlapX1
&&
overlapY2
>
overlapY1
) {
overlapArea
= (
overlapX2
-
overlapX1
) * (
overlapY2
-
overlapY1
);
}
// 返回总面积 = 两个矩形的面积和 - 重叠区域的面积
return
area1
+
area2
-
overlapArea
;
}
public
static
void
main
(
String
[]
args
) {
L0223_RectangleArea
solution
=
new
L0223_RectangleArea
();
// 测试用例 1
System
.
out
.
println
(
solution
.
computeArea
(-
3
,
0
,
3
,
4
,
0
, -
1
,
9
,
2
));
// 预期输出:45
// 测试用例 2
System
.
out
.
println
(
solution
.
computeArea
(-
2
, -
2
,
2
,
2
, -
2
, -
2
,
2
,
2
));
// 预期输出:16
}
}
Back
|
FazBrowse Home
|
New Git URL