未验证 提交 6935b73e 编写于 作者: B BruceCat 提交者: GitHub

【986.区间列表的交集】【Java】

【986.区间列表的交集】【Java】
......@@ -133,4 +133,27 @@ def intervalIntersection(A, B):
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
[KiraZh](https://github.com/KiraZh)提供第986题Java代码
```java
class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
List<int[]> res = new ArrayList<>();
int a = 0, b = 0;
while(a < A.length && b < B.length) {
// 确定左边界,两个区间左边界的最大值
int left = Math.max(A[a][0], B[b][0]);
// 确定右边界,两个区间右边界的最小值
int right = Math.min(A[a][1], B[b][1]);
// 左边界小于右边界则加入结果集
if (left <= right)
res.add(new int[] {left, right});
// 右边界更大的保持不动,另一个指针移动,继续比较
if(A[a][1] < B[b][1]) a++;
else b++;
}
// 将结果转为数组
return res.toArray(new int[0][]);
}
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册