永川区网站建设咨询,编程入门自学软件,发免费广告电话号码,网站建设部工作职能参考资料#xff1a;代码随想录
题目链接#xff1a;. - 力扣#xff08;LeetCode#xff09;
做过用最少数量的箭引爆气球和无重叠区间这两道题目后#xff0c;题意和题解都不难理解。唯一的一点儿难点是对于api的运用。
class Solution {public int[][] merge(int[][…参考资料代码随想录
题目链接. - 力扣LeetCode
做过用最少数量的箭引爆气球和无重叠区间这两道题目后题意和题解都不难理解。唯一的一点儿难点是对于api的运用。
class Solution {public int[][] merge(int[][] intervals) {if(intervals.length 0) return null;Arrays.sort(intervals,(a,b)-Integer.compare(a[0],b[0]));//Listint[] res new ArrayList();Listint[] res new LinkedList();//默认有一个区间res.add(intervals[0]);//遍历区间for(int i 1;i intervals.length;i){//判断是否是重复区间if(intervals[i][0] res.getLast()[1]){//进行合并操作int begin res.get(i-1)[0];int end Math.max(intervals[i][1],res.get(i-1)[1]);res.removeLast();res.add(new int[]{begin,end});}else{//不重复则直接放入res.add(intervals[i]);}}return res.toArray(new int[res.size()][]);}
}