给定两个由闭区间组成的列表 first 和 second ,其中 , 表示区间,保证这两个列表内部是不存在交集的,并且是排序之后的。 请找出两个区间列表的交集。 数据范围:区间列表的长度满足 , 区间列表中的值满足
区块链毕设网qklbishe.com为您提供问题的解答
给定两个由闭区间组成的列表 first 和 second ,其中
,
表示区间,保证这两个列表内部是不存在交集的,并且是排序之后的。
请找出两个区间列表的交集。
数据范围:区间列表的长度满足
, 区间列表中的值满足
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param first int整型ArrayList<ArrayList<>> * @param second int整型ArrayList<ArrayList<>> * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> intersectionofintervals(ArrayList<ArrayList<Integer>> first, ArrayList<ArrayList<Integer>> second) { ArrayList<ArrayList<Integer>> result = new ArrayList<>(); // write code here int firstIndex = 0; int secondIndex = 0; while (firstIndex < first.size() && secondIndex < second.size()) { Integer firstStart = first.get(firstIndex).get(0); Integer firstEnd = first.get(firstIndex).get(1); Integer secondStart = second.get(secondIndex).get(0); Integer secondEnd = second.get(secondIndex).get(1); if (firstStart > secondEnd) { secondIndex++; } else if (firstEnd < secondStart) { firstIndex++; } else { ArrayList<Integer> temp = new ArrayList<>(); temp.add(Math.max(firstStart, secondStart)); if (firstEnd < secondEnd) { temp.add(firstEnd); firstIndex++; } else { temp.add(secondEnd); secondIndex++; } result.add(temp); } } return result; } }
09:09
以上就是关于问题给定两个由闭区间组成的列表 first 和 second ,其中 , 表示区间,保证这两个列表内部是不存在交集的,并且是排序之后的。
请找出两个区间列表的交集。
数据范围:区间列表的长度满足 , 区间列表中的值满足的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训