[a,b],[c,d]..................... 先按两个区间做,多区间的话就是重复过程 class MyNum{ //本数据类型自己定义,内含2个int 分别表示区间 起始 和 结束 ; private int start, private int end, public int getStart(){} // 下边就不写了 无非是get set 方法 public void setStart(int i ){ start = i ;} ................................ } public MyNum method(int first_start, int first_end, int second_start, int second_end){ MyNum mn = new MyNum(); mn.setStart(first_start > second_start ? first_start : second_start); mn.setEnd(first_end < second_end ? first_end : second_end); retrun mn ; }
哦 上边忘写判断条件了 public MyNum method(int first_start, int first_end, int second_start, int second_end){ if(first_start > second _end || first_end < second_start){ return null; } MyNum mn = new MyNum(); mn.setStart(first_start > second_start ? first_start : second_start); mn.setEnd(first_end < second_end ? first_end : second_end); return mn ; } 所以根据判断返回的对象是否为NULL 就可以知道2个区间的交集是否为空,不为空的话区间是多少。 3个区间的话就是 前2个区间交集与第3个再交集 类推下去。。 A 交 B 交 C == (A 交 B)交 C
比如 [a, b] [c, d] [e, f] 先找出 a, c, e中最大的数 max 再找出 b, d, f中最小的数 min 如果max <= min 那么 [max, min]中都是重复的数字 若 max > min 则没有重复的数字
2、先按起始的数排序 假设a<c
[a,b]
[c,d]
3、再判断c<b是否成立 如果不成立 输出:无重复数字
如果成立 得到新区间[c,b>d?d:b] 然后和接下去的区间一起重复步骤1
4、读完全部区间得到重复数字
先按两个区间做,多区间的话就是重复过程
class MyNum{ //本数据类型自己定义,内含2个int 分别表示区间 起始 和 结束 ;
private int start,
private int end,
public int getStart(){} // 下边就不写了 无非是get set 方法
public void setStart(int i ){ start = i ;}
................................
}
public MyNum method(int first_start, int first_end, int second_start, int second_end){
MyNum mn = new MyNum();
mn.setStart(first_start > second_start ? first_start : second_start);
mn.setEnd(first_end < second_end ? first_end : second_end);
retrun mn ;
}
public MyNum method(int first_start, int first_end, int second_start, int second_end){
if(first_start > second _end || first_end < second_start){
return null;
}
MyNum mn = new MyNum();
mn.setStart(first_start > second_start ? first_start : second_start);
mn.setEnd(first_end < second_end ? first_end : second_end);
return mn ;
}
所以根据判断返回的对象是否为NULL 就可以知道2个区间的交集是否为空,不为空的话区间是多少。
3个区间的话就是 前2个区间交集与第3个再交集 类推下去。。
A 交 B 交 C == (A 交 B)交 C
[a, b]
[c, d]
[e, f]
先找出 a, c, e中最大的数 max
再找出 b, d, f中最小的数 min
如果max <= min
那么 [max, min]中都是重复的数字
若 max > min 则没有重复的数字
import java.util.List;public class RepeatedlySpace { public static void main(String[] args) {
// 测试数据
int[] arr1 = { 20, 50 };
int[] arr2 = { 25, 60 };
int[] arr3 = { 10, 90 };
// arr1 arr2 arr3 存在重复数字 区间为 25 ~ 50
// 1 ~ 24 或者 51 ~ 100做测试
// int[] arr4 = { 1, 24 };
int[] arr4 = { 51, 100 };
List<int[]> list = new ArrayList<int[]>();
list.add(arr1);
list.add(arr2);
list.add(arr3);
list.add(arr4); System.out
.println(isExists(getMinAndMaxNum(list), list) ? "存在" : "不存在");
} /*
* 找出最小值中的最大元素, 找出最大值中的最小元素, 组成比较数组
*/
public static int[] getMinAndMaxNum(List<int[]> list) { int[] minAndMax = new int[2];
int tempNumA = 0;
int tempNumB = 0;
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
{ /* 找出最小 */
tempNumA = list.get(i)[0];
tempNumB = list.get(j)[0];
minAndMax[0] = tempNumA > tempNumB ? tempNumA : tempNumB;
/* 找出最大 */
tempNumA = list.get(i)[1];
tempNumB = list.get(j)[1];
minAndMax[1] = tempNumA > tempNumB ? tempNumB : tempNumA;
}
}
}
return minAndMax;
} /*
* 最大元素小于比较数组最小元素, 或者 最小元素大于比较数组最大元素, 则不存在重复数字返回false,
*/
public static boolean isExists(int[] num, List<int[]> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i)[0] > num[1] || list.get(i)[1] < num[0]) {
return false;
}
}
return true;
}
}
public class RepeatedlySpace { public static void main(String[] args) {
// 测试数据
int[] arr1 = { 20, 50 };
int[] arr2 = { 25, 60 };
int[] arr3 = { 10, 90 };
// arr1 arr2 arr3 存在重复数字 区间为 25 ~ 50
// 1 ~ 24 或者 51 ~ 100做测试
int[] arr4 = { 1, 24 };
//int[] arr4 = { 51, 100 };
List<int[]> list = new ArrayList<int[]>();
list.add(arr1);
list.add(arr2);
list.add(arr3);
list.add(arr4); System.out.println(isExists(list)? "存在" : "不存在");
}
public static boolean isExists(List<int []> list){
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if(list.get(i)[0]>list.get(j)[1]||list.get(i)[1]<list.get(j)[0]) return false;
}
}
return true;
}
}