算法 n1段区间 与 n2段区间求交集
来源:互联网 发布:android滑动解锁源码 编辑:程序博客网 时间:2024/05/18 02:01
如下所示:
1 3 4 9 11 13
n1段区间之间互无交集,|——| |——————| |————|
n2段区间之间互无交集 |————| |——| |————| |——————|
2 5 6 7 8 10 12 14
仅通过图示来看很容易得到交集的答案,交集是[2,3], [4,5], [6,7], [8,9], [12,13],但是当区间很多时如何去求?
首先定义两个List<Long> list1=new ArrayList<Long>(); List<Long> list2=new ArrayList<Long>();
list1放置n1段区间的起始终止点 :list1.add(1);list1.add(3);...
list2放置n2段区间的起始终止点:list2.add(2);list2.add(5);.....
然后调用函数:
public List<Long> GetIntersection(List<Long> list1, List<Long> list2){List<Long> intersection=new ArrayList<Long>();for(int i=0;i<list1.size();i+=2){Long start1=list1.get(i); Long end1=list1.get(i+1);for(int j=0;j<list2.size();j+=2){Long start2=list2.get(j); Long end2=list2.get(j+1);// |______| |______|// |______| or |______|if(end1<=start2 || end2<=start1){continue;}if(start2>start1){ if(end1<end2){ // |______|// |______|intersection.add(start2); intersection.add(end1);}else{// |______|// |____|intersection.add(start2); intersection.add(end2);}}else{if(end1<end2){ // |___|// |______|intersection.add(start1); intersection.add(end1);}else{// |______|// |____|intersection.add(start1); intersection.add(end2);}}}}return intersection;}
阅读全文
0 0
- 算法 n1段区间 与 n2段区间求交集
- 区间求交集算法
- 给定一个正整数n,将其分成m段,每段为n1,n2,...,nm,求怎么划分使得n1*n2*...*nm最大
- 区间段问题
- hdu 4578 线段树 成段+ 成段* 成段替换 区间求平方和 立方和
- 在一维坐标轴上有n个区间段,求重合区间最长的两个区间段。
- 在一维坐标轴上有n个区间段,求重合区间最长的两个区间段
- 5.在一维坐标轴上有n个区间段,求重合区间最长的两个区间段。
- 在一维坐标轴上有n个区间段,求重合区间最长的两个区间段。
- zoj 1610 Count the Colors 【区间覆盖 求染色段】
- 区间合并 成段异或更新 求最长01段
- 求区间最大子段和(线段树)
- 输入两个ip区间段,判断是否有交集【2018华为软件岗位笔试题目】
- POJ3468 区间成段增减
- HDU1698 区间成段替换
- NYOJ 746 - 正整数n划分为m段,求m段的最大乘积 【区间DP】
- PID13 / 校门外的树 ☆(m段区间求区间并长度)
- 重合区间最长的两个区间段SegmentOverlap
- LeetCode 1. Two Sum
- c++入门教程(九)
- 算法 数学区间求并集
- C++/Python/Matlab执行效率分析
- java版poi+excel导入树形数据实例2
- 算法 n1段区间 与 n2段区间求交集
- 磁盘读写原理
- 慧荣PT项目见疑
- C# winform 中MessageBox用法大全(附效果图)
- python笔记
- Codeforces Round #386 (Div. 2) D. Green and Black Tea
- linux学习之旅(十九)&高级网络配置
- 序列与元组_笔记
- LeetCode之Count and Say