提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、力扣435. 无重叠区间
- 二、力扣763. 划分字母区间
- 三、力扣56. 合并区间
前言
一、力扣435. 无重叠区间
class Solution {public int eraseOverlapIntervals(int[][] intervals) {Arrays.sort(intervals, (a,b) -> Integer.compare(a[0],b[0]));int count = 1, left = intervals[0][0], right = intervals[0][1];for(int i = 1; i < intervals.length; i ++){if(intervals[i][0] < right){right = Math.min(right, intervals[i][1]);}else{count ++;right = intervals[i][1];}}return intervals.length - count;}
}
二、力扣763. 划分字母区间
class Solution {public List<Integer> partitionLabels(String s) {List<Integer> list = new ArrayList<>();char[] ch = s.toCharArray();int[] edge = new int[27];for(int i = 0; i < ch.length; i ++){edge[ch[i] - 'a'] = i;}int last = -1, idx = 0;for(int i = 0; i < ch.length; i ++){idx = Math.max(idx, edge[ch[i] - 'a']);if(i == idx){list.add(i - last);last = i;}}return list;}
}
三、力扣56. 合并区间
class Solution {public int[][] merge(int[][] intervals) {List<int[]> res = new ArrayList<>();Arrays.sort(intervals ,(a,b)->Integer.compare(a[0],b[0]));int left = intervals[0][0], right = intervals[0][1];for(int i = 1; i < intervals.length; i ++){if(intervals[i][0] > right){res.add(new int[]{left, right});left = intervals[i][0];right = intervals[i][1];}else{right = Math.max(right, intervals[i][1]);}}res.add(new int[]{left, right});return res.toArray(new int[res.size()][]);}
}