当前位置:网站首页>Intersection of interval lists
Intersection of interval lists
2022-04-22 06:40:00 【Da Da meat bun】
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Intersection of interval list {
public static void main(String[] args) {
int[][] firstList = {
{0,2},{5,10},{13,23},{24,25}};
int[][] secondList = {
{1,5},{8,12},{15,24},{25,26}};
int[][] merge = intervalIntersection(firstList,secondList);
for (int[] a:merge) {
System.out.println(Arrays.toString(a));
}
}
public static int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
int len1=firstList.length;
int len2=secondList.length;
if(len1==0 || len2==0)
return new int[0][2];
int i=0;
int j=0;
List<int[]> list=new ArrayList<int[]>();
while (i<len1 && j<len2){
int start=Math.max(firstList[i][0],secondList[j][0]);
int end=Math.min(firstList[i][1],secondList[j][1]);
if(start<=end)// If there is an intersection , Preservation
list.add(new int[]{start,end});
if(firstList[i][1]<secondList[j][1])
i++;
else
j++;
}
return list.toArray(new int[list.size()][2]);
}
}
/*
Given two by some Closed interval A list of components ,firstList and secondList , among firstList[i] = [starti, endi] and secondList[j] = [startj, endj]
. Each interval list is paired Disjoint Of , also It's sorted . Back here The intersection of two interval lists .
Formally , Closed interval [a, b]( among a <= b) For real numbers x Set , and a <= x <= b .
Of two closed intervals intersection Is a set of real numbers , Or it's an empty set , Either it's a closed interval . for example ,[1, 3] and [2, 4] The intersection of is [2, 3] .
Example 1:
Input :firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
Output :[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Example 2:
Input :firstList = [[1,3],[5,9]], secondList = []
Output :[]
Example 3:
Input :firstList = [], secondList = [[4,8],[10,12]]
Output :[]
Example 4:
Input :firstList = [[1,7]], secondList = [[3,10]]
Output :[[3,7]]
*/
版权声明
本文为[Da Da meat bun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220553530597.html
边栏推荐
- 剑指offer:对称的二叉树(递归 迭代leetcode 101)
- The minors protection solution of digital beauty technology is heavily launched, opening a new era of minors' network escort
- 数美科技六周年 | 致敬背后的你
- 数美科技获得ISO/IEC 27701隐私信息管理体系国际认证
- 事务不生效之this调用
- Use PG_ STAT_ Replication monitoring replication
- logrus设置日志格式与输出函数名
- 创新实训(十)
- 8张图让你一步步看清 async/await 和 promise 的执行顺序
- 关于一段小程序的思考
猜你喜欢

OLAP和OLTP的区别及所对应模型(基础知识)

Iframe子父级传参

Mysql 根据某一列的值 循环添加序号

js引擎的循环机制:同步,异步,事件循环

Shumei technology was invited to participate in the content governance label seminar of ICT Institute

利用win自带功能让处于同一局域网的两个电脑之间互传文件(速度和本地磁盘间互传相同)

webService接口编写并发布与webService接口的调用(一)

创新实训(九)整合

Solve the problem of error in installing PostgreSQL under windows2012 R2

congratulations! China Mobile Smart Home Center was selected in the list of ICT industry in 2020
随机推荐
The difference between hash mode and history mode
Generation principle and recurrence of epic vulnerability log4j2 that is popular in it circle
数美2022数字风控峰会开启,五大看点提前曝光
uglifyjs压缩JS
使用AES加密-复用前人的智慧
2021-07-07
剑指offer:二叉树中和为某一值的路径(回溯)
Flink源码之StreamExecutionEnvironment
Digital beauty technology won the "top ten intelligent risk control management innovation award" of the banker
MYSQL之高性能索引
数美科技社交行业未成年人保护解决方案:守护未成年人的“社交圈”
一套sql语句同时支持Oracle跟Mysql?
Shumei technology was honored as the "top 100 scientific and technological innovation of private enterprises in Beijing"
A series of interpretations of the general data protection regulations (gdpr): how to judge whether offshore enterprises are under the jurisdiction of gdpr?
《信息系统项目管理师总结》第三章 项目进度管理
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/?k=“
MySQL中的锁与事务
数美科技未成年人保护解决方案重磅上线,开启未成年人网络护航新时代
MySQL备忘录(供自己查询所用)
MySQL basics 2