Skip to content

Commit 3fbaf79

Browse files
committed
一刷986
1 parent 5d8ddd7 commit 3fbaf79

File tree

7 files changed

+125
-29
lines changed

7 files changed

+125
-29
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,14 +6927,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
69276927
//|{doc_base_url}/0985-sum-of-even-numbers-after-queries.adoc[题解]
69286928
//|Easy
69296929
//|
6930-
//
6931-
//|{counter:codes}
6932-
//|{leetcode_base_url}/interval-list-intersections/[986. Interval List Intersections^]
6933-
//|{source_base_url}/_0986_IntervalListIntersections.java[Java]
6934-
//|{doc_base_url}/0986-interval-list-intersections.adoc[题解]
6935-
//|Medium
6936-
//|
6937-
//
6930+
6931+
|{counter:codes}
6932+
|{leetcode_base_url}/interval-list-intersections/[986. Interval List Intersections^]
6933+
|{source_base_url}/_0986_IntervalListIntersections.java[Java]
6934+
|{doc_base_url}/0986-interval-list-intersections.adoc[题解]
6935+
|Medium
6936+
|
6937+
69386938
//|{counter:codes}
69396939
//|{leetcode_base_url}/vertical-order-traversal-of-a-binary-tree/[987. Vertical Order Traversal of a Binary Tree^]
69406940
//|{source_base_url}/_0987_VerticalOrderTraversalOfABinaryTree.java[Java]
Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,91 @@
11
[#0986-interval-list-intersections]
2-
= 986. Interval List Intersections
2+
= 986. 区间列表的交集
33

4-
{leetcode}/problems/interval-list-intersections/[LeetCode - Interval List Intersections^]
4+
https://leetcode.cn/problems/interval-list-intersections/[LeetCode - 986. 区间列表的交集 ^]
55

6-
Given two lists of *closed* intervals, each list of intervals is pairwise disjoint and in sorted order.
6+
给定两个由一些 *闭区间* 组成的列表,`firstList``secondList`,其中 `firstList[i] = [start~i~, end~i~]``secondList[j] = [start~j~, end~j~]`。每个区间列表都是成对 *不相交* 的,并且 *已经排序*
77

8-
Return the intersection of these two interval lists.
8+
返回这 *两个区间列表的交集*
99

10-
_(Formally, a closed interval `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)_
10+
形式上,*闭区间* `[a, b]`(其中 `+a <= b+`)表示实数 `x` 的集合,而 `+a <= x <= b+`
1111

12+
两个闭区间的 *交集*是一组实数,要么为空集,要么为闭区间。例如,`[1, 3]``[2, 4]` 的交集为 `[2, 3]`
1213

13-
14+
*示例 1:*
1415

15-
*Example 1:*
16+
image::images/0986-01.png[{image_attr}]
1617

17-
*image::https://assets.leetcode.com/uploads/2019/01/30/interval1.png[]*
18+
image::images/0056-01.png[{image_attr}]
1819

19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
22-
*Output:* [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
23-
*Reminder:* The inputs and the desired output are lists of Interval objects, and not arrays or lists.
24-
----
20+
....
21+
输入:firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
22+
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
23+
....
24+
25+
*示例 2:*
26+
27+
....
28+
输入:firstList = [[1,3],[5,9]], secondList = []
29+
输出:[]
30+
....
31+
32+
*示例 3:*
2533

26-
34+
....
35+
输入:firstList = [], secondList = [[4,8],[10,12]]
36+
输出:[]
37+
....
2738

28-
*Note:*
39+
*示例 4:*
2940

41+
....
42+
输入:firstList = [[1,7]], secondList = [[3,10]]
43+
输出:[[3,7]]
44+
....
3045

31-
. `0 <= A.length < 1000`
32-
. `0 <= B.length < 1000`
33-
. `0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9`
46+
*提示:*
3447

48+
* `+0 <= firstList.length, secondList.length <= 1000+`
49+
* `+firstList.length + secondList.length >= 1+`
50+
* `0 \<= start~i~ < end~i~ \<= 10^9^`
51+
* `end~i~ < start~i+1~`
52+
* `0 \<= start~j~ < end~j~ \<= 10^9^`
53+
* `end~j~ < start~j+1~`
3554
36-
*NOTE:* input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
3755
3856
57+
== 思路分析
3958

59+
双指针。先排除两种没有交集的情况,剩下四种有交集的情况,左边选两个里面最大的,右边选两个里面最小的。哪个小,向右移动哪个的指针。
60+
61+
官方题解代码更简洁,直接左边选两个里面最大的,右边选两个里面最小的。然后再检查这两个值是否左小右大。也是“哪个小,向右移动哪个的指针。”。
62+
63+
image::images/0986-10.png[{image_attr}]
4064

4165
[[src-0986]]
66+
[tabs]
67+
====
68+
一刷::
69+
+
70+
--
4271
[{java_src_attr}]
4372
----
4473
include::{sourcedir}/_0986_IntervalListIntersections.java[tag=answer]
4574
----
75+
--
76+
77+
// 二刷::
78+
// +
79+
// --
80+
// [{java_src_attr}]
81+
// ----
82+
// include::{sourcedir}/_0986_IntervalListIntersections_2.java[tag=answer]
83+
// ----
84+
// --
85+
====
86+
87+
88+
== 参考资料
4689

90+
. https://leetcode.cn/problems/interval-list-intersections/solutions/3604/qu-jian-lie-biao-de-jiao-ji-by-leetcode/[986. 区间列表的交集 - 官方题解^]
91+
. https://leetcode.cn/problems/interval-list-intersections/solutions/1213336/qu-jian-lie-biao-de-jiao-ji-cyu-yan-xian-n901/[986. 区间列表的交集 - 【C语言详解】【超级详细】^]

docs/images/0986-01.png

30.7 KB
Loading

docs/images/0986-10.png

64.6 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ include::0980-unique-paths-iii.adoc[leveloffset=+1]
20552055

20562056
// include::0985-sum-of-even-numbers-after-queries.adoc[leveloffset=+1]
20572057

2058-
// include::0986-interval-list-intersections.adoc[leveloffset=+1]
2058+
include::0986-interval-list-intersections.adoc[leveloffset=+1]
20592059

20602060
// include::0987-vertical-order-traversal-of-a-binary-tree.adoc[leveloffset=+1]
20612061

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@ endif::[]
898898
|{doc_base_url}/1209-remove-all-adjacent-duplicates-in-string-ii.adoc[题解]
899899
|✅ 暴力解法:对重复的相邻字母计数,当计数达到 k 时将其删除。重复此操作,直到没有删除的字符为止。通过 19 / 21 个测试用例。更优解:把当前字符的次数存下来,下一个字符就可以在当前字符基础上做处理,省去重复计算。官方题解提供了多种解法,非常优秀!
900900

901+
|{counter:codes2503}
902+
|{leetcode_base_url}/interval-list-intersections/[986. 区间列表的交集^]
903+
|{doc_base_url}/0986-interval-list-intersections.adoc[题解]
904+
|✅ 双指针。先排除两种没有交集的情况,剩下四种有交集的情况,左边选两个里面最大的,右边选两个里面最小的。哪个小,向右移动哪个的指针。
905+
901906

902907
|===
903908

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0986_IntervalListIntersections {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-03 14:23:09
11+
*/
12+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
13+
if (firstList == null || secondList == null
14+
|| firstList.length == 0 || secondList.length == 0) {
15+
return new int[0][0];
16+
}
17+
int first = 0, second = 0;
18+
List<int[]> result = new ArrayList<>();
19+
while (first < firstList.length && second < secondList.length) {
20+
if (firstList[first][1] < secondList[second][0]) {
21+
first++;
22+
} else if (secondList[second][1] < firstList[first][0]) {
23+
second++;
24+
} else {
25+
int max = Math.max(firstList[first][0], secondList[second][0]);
26+
int min;
27+
if (firstList[first][1] > secondList[second][1]) {
28+
min = secondList[second][1];
29+
second++;
30+
} else {
31+
min = firstList[first][1];
32+
first++;
33+
}
34+
result.add(new int[]{max, min});
35+
}
36+
}
37+
return result.toArray(new int[][]{});
38+
}
39+
// end::answer[]
40+
41+
public static void main(String[] args) {
42+
new _0986_IntervalListIntersections()
43+
.intervalIntersection(new int[][]{{0, 2}, {5, 10}, {13, 23}, {24, 25}},
44+
new int[][]{{1, 5}, {8, 12}, {15, 24}, {25, 26}});
45+
}
46+
}

0 commit comments

Comments
 (0)