35. 无重叠区间
题目链接:435. 无重叠区间 - 力扣(LeetCode)
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (x1, x2) -> {
if (x1[0] != x2[0]) {
return x1[0] - x2[0];
} else {
return x1[1] - x2[1];
}
});
int ret = 0;
for(int i = 0; i < intervals.length - 1; ++i) {
if(intervals[i][1] > intervals[i+1][0]) {
intervals[i + 1][1] = Math.min(intervals[i][1], intervals[i+1][1]);
ret++;
}
}
return ret;
}
}
763.划分字母区间
题目链接:763. 划分字母区间 - 力扣(LeetCode)
class Solution {
public List<Integer> partitionLabels(String s) {
int[] a = new int[26];
int[] b = new int[26];
for(int i = 0; i < s.length(); ++i) {
++a[s.charAt(i)-'a'];
}
List<Integer> ans = new ArrayList<>();
int differ = 0, len = 0;
for(int i = 0; i < s.length(); ++i) {
++len;
int index = s.charAt(i) - 'a';
++b[index];
if(b[index] == 1) {
++differ;
}
if(b[index] == a[index]) {
--differ;
}
if(differ == 0) {
ans.add(len);
len = 0;
}
}
return ans;
}
}
56. 合并区间
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] a,int[] b){
return a[0] - b[0];
}
});
List<int []> list = new ArrayList<>();
for(int i = 0; i < intervals.length; ++i) {
int L = intervals[i][0], R = intervals[i][1];
if(list.isEmpty() || list.get(list.size()-1)[1] < L) {
list.add(intervals[i]);
}else {
list.get(list.size()-1)[1] = Math.max(list.get(list.size()-1)[1], R);
}
}
return list.toArray(new int[list.size()][]);
}
}