56. Merge Intervals
作者:互联网
My PriorityQueue Solution:
class Solution { public int[][] merge(int[][] intervals) { PriorityQueue<int[]> queue = new PriorityQueue<>((a,b)->a[0]-b[0]); for(int[] inter: intervals){ queue.offer(inter); } List<int[]> res = new ArrayList<>(); while(!queue.isEmpty()){ int[] first = queue.poll(); if(!queue.isEmpty()){ int[] next = queue.poll(); if(first[1]>=next[0]){ queue.offer(new int[]{first[0], Math.max(first[1], next[1])}); } else{ res.add(first); queue.offer(next); } }else{ res.add(first); } } return res.toArray() int[][] resA = new int[res.size()][2]; for(int i=0;i<res.size();i++){ resA[i]=res.get(i); } return resA; } }
标签:int,res,56,next,queue,Merge,Intervals,new,first 来源: https://www.cnblogs.com/feiflytech/p/16110611.html