区间重合判断 java_java判断多个区间是否有重合
importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;/*** @description: 区间工具类
*@author: wyj
* @time: 2020/3/1 15:05*/
public classSectionUtil {//最小值
privateString min_entity;//最大值
privateString max_entity;//左侧括号状态:false -开区间 true-- 闭区间
private boolean left_sate = false;//右侧括号状态:false -开区间 true-- 闭区间
private boolean right_sate = false;privateSectionUtil() {
}public SectionUtil(String min_entity, String max_entity, boolean left_sate, booleanright_sate) {this.min_entity =min_entity;this.max_entity =max_entity;this.left_sate =left_sate;this.right_sate =right_sate;
}publicString getMin_entity() {returnmin_entity;
}publicString getMax_entity() {returnmax_entity;
}public booleanisLeft_sate() {returnleft_sate;
}public booleanisRight_sate() {returnright_sate;
}/*** @description: 创建负区间((负无穷,X])
*@paramvalue 区间最大值
*@paramright_sate 区间开闭状态
* @Date: 2020/3/2 14:37*/
public static SectionUtil creatFu(String value, booleanright_sate) {return new SectionUtil("", value, false, right_sate);
}/*** @description: 创建正区间[X,正无穷))
*@paramvalue 区间最小值
*@paramleft_sate 区间开闭状态
* @Date: 2020/3/2 14:37*/
public static SectionUtil creatZheng(String value, booleanleft_sate) {return new SectionUtil(value, "", left_sate, false);
}/*** @description: 创建闭合区间([X,Y])
*@parammin 区间最小值
*@parammax 区间最大值
*@paramleft_sate 区间左侧开闭状态
*@paramright_sate 区间右侧开闭状态
*@return* @Date: 2020/3/2 14:41*/
public static SectionUtil creat(String min, boolean left_sate, String max, booleanright_sate) {return newSectionUtil(min, max, left_sate, right_sate);
}/*** @description: 将实体类转换成区间集合
*@paramrecord 待转换的实体类
*@return转换后的区间集合类(不等于时转换后为2个区间,所以采用集合)
* @Date: 2020/3/2 14:19*/
public static ListgetSections(ReqRespResearchProductQuestionnaireItem record) {
List list = new ArrayList<>();
String record_max=record.getMaxValue();
String record_min=record.getMinValue();switch(record.getSymbol()) {case 1:
list.add(creatZheng(record_max,false));break;case 2:
list.add(creatFu(record_max,false));break;case 3:
list.add(creat(record_max,true, record_max, true));break;case 4:
list.add(creatFu(record_max,false));
list.add(creatZheng(record_max,false));break;case 5:
list.add(creatZheng(record_max,true));break;case 6:
list.add(creatFu(record_max,true));break;case 7:
list.add(creat(record_min,true, record_max, true));break;case 8:
list.add(creat(record_min,true, record_max, false));break;case 9:
list.add(creat(record_min,false, record_max, true));break;case 10:
list.add(creat(record_min,false, record_max, false));break;
}returnlist;
}public intcompareTo(String first_value, String second_value) {//first_value为空表示为正无穷,second_value为空表示为负无穷
if (isBlank(first_value) ||isBlank(second_value)) {return 1;
}returncompareToValue(first_value,second_value);
}//判断字符串是否为空
public static booleanisBlank(String str) {intstrLen;if (str == null || (strLen = str.length()) == 0) {return true;
}for (int i = 0; i < strLen; i++) {if ((Character.isWhitespace(str.charAt(i)) == false)) {return false;
}
}return true;
}/***@paramrecord 判断区间是否有重合
*@returntrue-有重合 false -无重合
* @description: 判断当前区间是否和指定区间重合
* @Date: 2020/3/2 10:20*/
public booleanisChonghe(SectionUtil record) {
String min_entity=record.getMin_entity();
String max_entity=record.getMax_entity();boolean left_sate =record.isLeft_sate();boolean right_sate =record.isRight_sate();boolean left_isok = false;boolean right_isok = false;//重合条件,第一个区间最大值大于第二个区间最小值并且第一个区间的最小值小于第二个区间的最大值//注意传值顺序,第一个值为第一个区间的最大值(此处不能反)
int first_result = compareTo(this.max_entity, min_entity);if ((first_result == 0 && this.right_sate && left_sate) || (first_result > 0)) {
left_isok= true;
}//注意传值顺序,第一个值为第二个区间的最大值(此处不能反)
int second_result = compareTo(max_entity, this.min_entity);//此处本应该是second_result<0,但由于上一步参数传递时时反正传递,故此此处为second_result>0
if ((second_result == 0 && this.left_sate && right_sate) || second_result > 0) {
right_isok= true;
}return left_isok &&right_isok;
}/*** @description: 比较集合中区间是否有重叠
*@paramlist1 待比较集合1
*@paramlist2 待比较集合2
*@return* @Date: 2020/3/2 11:49*/
public static boolean isChonghe(List list1, Listlist2) {boolean chonghed = false;for(SectionUtil item1 : list1) {for(SectionUtil item2 : list2) {
chonghed=item1.isChonghe(item2);if(chonghed) {return true;
}
}
}returnchonghed;
}//比较大小
public static intcompareToValue(String value1, String value2) {
BigDecimal b1= newBigDecimal(value1);
BigDecimal b2= newBigDecimal(value2);returnb1.compareTo(b2);
}
}
区间重合判断 java_java判断多个区间是否有重合相关推荐
- mysql判断是否在日期区间_通过sql判断时间区间是否存在数据
在做项目的时候遇到过一个问题,用户需要获取当前月或者几个月的数据,但是有一个要求,如果已经存在一张单已经包含了这几个月的数据,那么就不能再提取到重复的数据. 其实这个问题,我做完了我的方式之后才发现, ...
- python判断序列值横穿整个区间的次数
这里需要用的集合的知识,一般python集合会用intervals库或portion库,这里我们用portion库 intervals库github:https://github.com/kveste ...
- js 日期比较大小,js判断日期是否在区间内,js判断时间段是否在另外一个时间段内...
/** * 日期解析,字符串转日期 * @param dateString 可以为2017-02-16,2017/02/16,2017.02.16 * @returns {Date} 返回对应的日期对 ...
- python判断当前时间是否在两个时间之间_Python 判断时间是否在时间区间内的实例...
判断时间是否在时间区间内 大家都知道 3<4<5这种连等式判断在python中是可行的 >>> 3<4<5 True 那么给定时间是否在时间区间内,也可以用连 ...
- java区间合并_贪心算法:合并区间
❝ 最近文章阅读量少了很多啊打卡也少了, 是不是年底了很多录友在忙期末考试啊,哈哈. 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: intervals = [[1,3],[2,6], ...
- 区间相关问题的学习: 最多不相交区间问题,区间选点问题与区间覆盖问题
区间相关问题包括: 1)最多不相交区间问题; 2)区间选点问题; 3)区间覆盖问题等. 最多不相交区间问题(又叫选择不相交区间,最大不相交覆盖等等),是指数轴上有n个开区间(a,b),选择尽量多个区间 ...
- 逻辑判断-if语句/文件目录属性判断/case判断
逻辑判断IF -gt :大于 -lt :小于 -eq :等于 -ne :不等于 -ge :大于或等于 -le :小于或等于 格式1: if 条件 :then 语句: fi a=5 if [ $a -g ...
- 【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )
题干: In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroe ...
- 动态分区分配算法代码_【代码】巩敦卫等TEVC论文:基于区间相似度分析的协同动态区间多目标进化优化算法...
分享代码:巩敦卫等TEVC论文:基于区间相似度分析的协同动态区间多目标进化优化算法. 说明:该代码基于Matlab2012a及Intlab5.5编写,对应文献:"Dunwei Gong, B ...
最新文章
- 授以渔 - Autodesk Forge 学习简谈 - 引言
- 2017 9月java答案_2017年9月计算机二级JAVA试题及答案
- leetcode 547. 省份数量(bfs)
- 简单的C语言程序合集-2
- feign 序列化_Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题...
- maven tomcat插件_Maven技术01
- Linux 二进制分析
- 根号二怎么不用计算机算,[转载]开根号,不用计算器你会么?
- 表格求和怎么操作?如何运用sumif函数进行求和
- Druid源码分析--整体结构
- 第五届上海市大学生网络安全大赛
- html5贝塞尔函数,径向偏振高阶贝塞尔-高斯涡旋光束的传输及其偏振特性
- 感性电路电流计算_第五讲 正弦交流电路分析
- ASEMI代理AD9833BRMZ-REEL原装ADI车规级AD9833BRMZ-REEL
- 支付系统就该这么设计(万能通用)
- php投影,使用 CSS 3 制作长投影
- Xshell4连接Linux系统中文显示乱码解决办法
- java 使用subList痛彻心扉的领悟
- Fabric CA国密版本的一种替代方案--使用cryptogen工具增加新用户
- 零基础怎么自学软件测试?分享五个宝藏网站,自学简直不要太轻松了