难度:
Hard
题意:
思路:
解法:
class Solution {
public int intersectionSizeTwo(int[][] intervals) {
Integer[] array = new Integer[intervals.length];
for (int i = 0;i < array.length;i++) {
array[i] = i;
}
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(intervals[o1][1], intervals[o2][1]);
}
});
List<Integer> result = new ArrayList<Integer>();
int[] t = new int[2];
for (int i = 0;i < array.length;i++) {
int n = 0;
if (result.size() > 0 && result.get(result.size() - 1) >= intervals[array[i]][0]) {
t[n++] = result.get(result.size() - 1);
}
if (result.size() > 1 && result.get(result.size() - 2) >= intervals[array[i]][0]) {
t[n++] = result.get(result.size() - 2);
}
int res = n;
int j = intervals[array[i]][1];
while (n != 2) {
boolean found = false;
for (int k = 0;k < n;k++) {
if (t[k] == j) {
found = true;
break;
}
}
if (found) {
j--;
} else {
t[n++] = j--;
}
}
if (res == 0) {
result.add(t[1]);
result.add(t[0]);
}
if (res == 1) {
result.add(t[1]);
}
}
return result.size();
}
}