986:区间列表的交集

This commit is contained in:
huangge1199 2021-08-12 16:17:24 +08:00
parent 097c487979
commit ec67005b7d
2 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,92 @@
//给定两个由一些 闭区间 组成的列表firstList secondList 其中 firstList[i] = [starti, endi] s
//econdList[j] = [startj, endj] 每个区间列表都是成对 不相交 并且 已经排序
//
// 返回这 两个区间列表的交集
//
// 形式上闭区间 [a, b]其中 a <= b表示实数 x 的集合 a <= x <= b
//
// 两个闭区间的 交集 是一组实数要么为空集要么为闭区间例如[1, 3] [2, 4] 的交集为 [2, 3]
//
//
//
// 示例 1
//
//
//输入firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,
//24],[25,26]]
//输出[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
//
//
// 示例 2
//
//
//输入firstList = [[1,3],[5,9]], secondList = []
//输出[]
//
//
// 示例 3
//
//
//输入firstList = [], secondList = [[4,8],[10,12]]
//输出[]
//
//
// 示例 4
//
//
//输入firstList = [[1,7]], secondList = [[3,10]]
//输出[[3,7]]
//
//
//
//
// 提示
//
//
// 0 <= firstList.length, secondList.length <= 1000
// firstList.length + secondList.length >= 1
// 0 <= starti < endi <= 109
// endi < starti+1
// 0 <= startj < endj <= 109
// endj < startj+1
//
// Related Topics 数组 双指针
// 👍 175 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
//986:区间列表的交集
class IntervalListIntersections {
public static void main(String[] args) {
//测试代码
Solution solution = new IntervalListIntersections().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
int firstIndex = 0;
int secondIndex = 0;
List<int[]> list = new ArrayList<>();
while (firstIndex < firstList.length && secondIndex < secondList.length) {
int start = Math.max(firstList[firstIndex][0], secondList[secondIndex][0]);
int end = Math.min(firstList[firstIndex][1], secondList[secondIndex][1]);
if (start <= end) {
list.add(new int[]{start, end});
}
if (firstList[firstIndex][1] < secondList[secondIndex][1]) {
firstIndex++;
} else {
secondIndex++;
}
}
return list.toArray(new int[list.size()][]);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,51 @@
<p>给定两个由一些<strong> 闭区间 </strong>组成的列表,<code>firstList</code><code>secondList</code> ,其中 <code>firstList[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 而 <code>secondList[j] = [start<sub>j</sub>, end<sub>j</sub>]</code> 。每个区间列表都是成对 <strong>不相交</strong> 的,并且 <strong>已经排序</strong></p>
<p>返回这 <strong>两个区间列表的交集</strong></p>
<p>形式上,<strong>闭区间</strong> <code>[a, b]</code>(其中 <code>a <= b</code>)表示实数 <code>x</code> 的集合,而 <code>a <= x <= b</code></p>
<p>两个闭区间的 <strong>交集</strong> 是一组实数,要么为空集,要么为闭区间。例如,<code>[1, 3]</code><code>[2, 4]</code> 的交集为 <code>[2, 3]</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/interval1.png" style="width: 700px; height: 194px;" />
<pre>
<strong>输入:</strong>firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
<strong>输出:</strong>[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>firstList = [[1,3],[5,9]], secondList = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>firstList = [], secondList = [[4,8],[10,12]]
<strong>输出:</strong>[]
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>firstList = [[1,7]], secondList = [[3,10]]
<strong>输出:</strong>[[3,7]]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= firstList.length, secondList.length <= 1000</code></li>
<li><code>firstList.length + secondList.length >= 1</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 10<sup>9</sup></code></li>
<li><code>end<sub>i</sub> < start<sub>i+1</sub></code></li>
<li><code>0 <= start<sub>j</sub> < end<sub>j</sub> <= 10<sup>9</sup> </code></li>
<li><code>end<sub>j</sub> < start<sub>j+1</sub></code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div>\n<div><li>👍 175</li><li>👎 0</li></div>