3 5
8 20
7 10

3 5 和8 20 不存在交集

3 5
8 20

9个回答

/**

• Created by admin on 2018/6/20.
*/
public class NumEntity {
private Integer x;
private Integer y;
public Integer getX() {
return x;
}

public void setX(Integer x) {
this.x = x;
}

public Integer getY() {
return y;
}

public void setY(Integer y) {
this.y = y;
}

public NumEntity(Integer x, Integer y) {
this.x = x;
this.y = y;
}

}

``````
``````

/**

• Created by admin on 2018/6/20.
*/
public class CsdnMain {

/**

• 主方法
• @param args */ public static void main(String[] args) { //传入数据为list的对象，为点结构 List list = new ArrayList<>(); list.add(new NumEntity(3,5)); list.add(new NumEntity(8,20)); list.add(new NumEntity(7,10)); //输出结果 System.out.println(checkCross(list)); }

/**

• 将传入的数据，小的在前，大的在后, 如果传入规则随机，则需要在list加入参数时做处理，如
• @param numEntity
• @return */ static NumEntity beforeDo(NumEntity numEntity){ numEntity.setX(Math.min(numEntity.getX(),numEntity.getY())); numEntity.setY(Math.max(numEntity.getX(),numEntity.getY())); return numEntity; }

/**

• 将传入的数据循环比较，设置temp为哨兵，如果最小值大于哨兵则将最大值赋于哨兵，再做下一次判断，如果最小值小于或者等于哨兵，则有交集
• @param list
• @return */ static boolean checkCross(List list){ int temp = 0; for(int i = 0;itemp){ temp = list.get(i).getY(); }else{ return false; } } return true; }

}

8 20和7 10为什么会存在交集，规则是什么

Boolean IsCross(int x1, int y1, int x2, int y2)
{
return (x1 <= x2 && y1 >= x2) || (x1 <= y2 && y1 >= y2) || (x1 >= x2 && y1 <= y2) || (x1 <= x2 && y1 >= y2);
}

`````` class Data implements Comparable<Data> {

private int id;
private int value;
private boolean isStart;

public Data(int value, boolean isStart, int id) {
this.value = value;
this.isStart = isStart;
this.id = id;
}

public int getId() {
return id;
}

public boolean isStart() {
return isStart;
}

public int getValue() {
return value;
}

@Override
public int compareTo(Data o) {
Data that = (Data)o;
if (this.value < that.value) {
return -1;
}
else if (this.value > that.value) {
return 1;
}
return 0;
}

}

public class Test {
public static void main(String[] args) {
Object[] data = Arrays.asList(new Data(3, true, 1), new Data(5, false, 1),
new Data(8, true, 2), new Data(20, false, 2),
new Data(7, true, 3), new Data(10, false, 3)
).toArray();

Arrays.sort(data);

boolean isIntercepted = false;
for (int i=0; i<data.length; i+=2) {
if (((Data)data[i]).getId() != ((Data)data[i+1]).getId() ||
!((Data)data[i]).isStart() ||
((Data)data[i+1]).isStart()) {
isIntercepted = true;
break;
}
}

System.out.println("data are intercepted: " + isIntercepted);
}
}
``````

minB <= minA <= maxB || minB <= maxA <= maxB || minA <= minB <= maxA || minA <= maxB <= maxA

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
private static List list = new ArrayList();
public static void main(String[] args) {
//添加数组

``````    //处理
System.out.println(dealList());
}

private static boolean dealList() {
for(Integer[] IntArr1 : list){
for(Integer[] IntArr2 : list){
Arrays.sort(IntArr1);
Arrays.sort(IntArr2);
if(IntArr1[0] == IntArr2[0] && IntArr1[1] == IntArr2[1]){
continue;
}
if(IntArr1[0] >= IntArr2[0] && IntArr1[0] <= IntArr2[1]){
return true;
}
if(IntArr1[1] >= IntArr2[0] && IntArr1[1] <= IntArr2[1]){
return true;
}
}
}
return false;
}
``````

}