Skip to content

Commit ad0ca9f

Browse files
committed
一刷223
1 parent 8de9140 commit ad0ca9f

File tree

8 files changed

+86
-20
lines changed

8 files changed

+86
-20
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,12 +1587,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
15871587
|Medium
15881588
|
15891589

1590-
//|{counter:codes}
1591-
//|{leetcode_base_url}/rectangle-area/[223. Rectangle Area^]
1592-
//|{source_base_url}/_0223_RectangleArea.java[Java]
1593-
//|{doc_base_url}/0223-rectangle-area.adoc[题解]
1594-
//|Medium
1595-
//|
1590+
|{counter:codes}
1591+
|{leetcode_base_url}/rectangle-area/[223. Rectangle Area^]
1592+
|{source_base_url}/_0223_RectangleArea.java[Java]
1593+
|{doc_base_url}/0223-rectangle-area.adoc[题解]
1594+
|Medium
1595+
|
15961596

15971597
|{counter:codes}
15981598
|{leetcode_base_url}/basic-calculator/[224. Basic Calculator^]

docs/0000-04-merge-intervals.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ image::images/0056-01.png[{image_attr}]
1818
. xref:0056-merge-intervals.adoc[56. Merge Intervals]
1919
. xref:0057-insert-interval.adoc[57. Insert Interval]
2020
. xref:0986-interval-list-intersections.adoc[986. Interval List Intersections]
21+
. xref:0223-rectangle-area.adoc[223. 矩形面积] -- 这道题不是区间合并类型的题目。但是,在处理相交面积时的手段和区间合并的手段是类似的!
2122
. xref:0252-meeting-rooms.adoc[252. Meeting Rooms]
2223
. xref:0253-meeting-rooms-ii.adoc[253. Meeting Rooms II]
2324
. xref:0452-minimum-number-of-arrows-to-burst-balloons.adoc[452. Minimum Number of Arrows to Burst Balloons]
2425
. xref:0759-employee-free-time.adoc[759. Employee Free Time]
2526
. xref:1094-car-pooling.adoc[1094. Car Pooling]
2627

2728

29+
2830
Conflicting Appointments (medium)

docs/0223-rectangle-area.adoc

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,70 @@
11
[#0223-rectangle-area]
2-
= 223. Rectangle Area
2+
= 223. 矩形面积
33

4-
{leetcode}/problems/rectangle-area/[LeetCode - Rectangle Area^]
4+
https://leetcode.cn/problems/rectangle-area/[LeetCode - 223. 矩形面积 ^]
55

6-
Find the total area covered by two *rectilinear* rectangles in a *2D* plane.
6+
给你 *二维* 平面上两个 *由直线构成且边与坐标轴平行/垂直* 的矩形,请你计算并返回两个矩形覆盖的总面积。
77

8-
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
8+
每个矩形由其 *左下* 顶点和 *右上* 顶点坐标表示:
99

10-
image::https://assets.leetcode.com/uploads/2018/10/22/rectangle_area.png[{image_attr}]
10+
* 第一个矩形由其左下顶点 `+(ax1, ay1)+` 和右上顶点 `+(ax2, ay2)+` 定义。
11+
* 第二个矩形由其左下顶点 `+(bx1, by1)+` 和右上顶点 `+(bx2, by2)+` 定义。
1112
12-
*Example:*
1313
14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
17-
*Output:* 45
18-
----
14+
*示例 1:*
15+
16+
image::images/0223-01.png[{image_attr}]
17+
18+
....
19+
输入:ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
20+
输出:45
21+
....
22+
23+
*示例 2:*
24+
25+
....
26+
输入:ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
27+
输出:16
28+
....
29+
1930

20-
*Note:*
31+
*提示:*
2132

22-
Assume that the total area is never beyond the maximum possible value of *int*.
33+
* `-10^4^ \<= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 \<= 10^4^`
2334
2435
2536
37+
== 思路分析
38+
39+
原本还想按分离,包含和相交三种情况来处理,提交后发现相交的情况有很多种,不好处理!
40+
41+
看题解,可以直接用容斥原理,与 xref:0000-04-merge-intervals.adoc[Merge Intervals 区间合并] 类型解题时,使用的处理手段类似,求出相交线段,即可求出重叠面积。
42+
43+
2644
[[src-0223]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
2750
[{java_src_attr}]
2851
----
2952
include::{sourcedir}/_0223_RectangleArea.java[tag=answer]
3053
----
54+
--
55+
56+
// 二刷::
57+
// +
58+
// --
59+
// [{java_src_attr}]
60+
// ----
61+
// include::{sourcedir}/_0223_RectangleArea_2.java[tag=answer]
62+
// ----
63+
// --
64+
====
65+
66+
67+
== 参考资料
3168

69+
. https://leetcode.cn/problems/rectangle-area/solutions/1024841/gong-shui-san-xie-yun-yong-rong-chi-yuan-hzit/[223. 矩形面积 - 【宫水三叶】运用容斥原理求解^]
70+
. https://leetcode.cn/problems/rectangle-area/solutions/1024639/ju-xing-mian-ji-by-leetcode-solution-xzbl/[223. 矩形面积 - 官方题解^]

docs/images/0223-01.png

50.6 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ include::0221-maximal-square.adoc[leveloffset=+1]
531531

532532
include::0222-count-complete-tree-nodes.adoc[leveloffset=+1]
533533

534-
// include::0223-rectangle-area.adoc[leveloffset=+1]
534+
include::0223-rectangle-area.adoc[leveloffset=+1]
535535

536536
include::0224-basic-calculator.adoc[leveloffset=+1]
537537

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,11 @@ endif::[]
11081108
|{doc_base_url}/0594-longest-harmonious-subsequence.adoc[题解]
11091109
|✅ 最简单的解法是排序。时间复杂度更优的解答是哈希计数。
11101110

1111+
|{counter:codes2503}
1112+
|{leetcode_base_url}/rectangle-area/[223. 矩形面积^]
1113+
|{doc_base_url}/0223-rectangle-area.adoc[题解]
1114+
|⭕️ 原本还想按分离,包含和相交三种情况来处理,相交情况太多,放弃!容斥原理的处理手段和 {doc_base_url}/0000-04-merge-intervals.adoc[Merge Intervals 区间合并] 题目的方式类似!
1115+
11111116
|===
11121117

11131118
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0222_CountCompleteTreeNodes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
public class _0222_CountCompleteTreeNodes {
1010
// tag::answer[]
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2020-01-28 12:21
14+
*/
1115
public int countNodes(TreeNode root) {
1216
if (root == null) {
1317
return 0;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0223_RectangleArea {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-07-01 17:28:11
9+
*/
10+
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
11+
int overx = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
12+
int overy = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
13+
return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - overx * overy;
14+
}
15+
// end::answer[]
16+
}

0 commit comments

Comments
 (0)