Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

56. Merge Intervals

Posted on 2020-07-16 | Edited on 2021-01-22

LeetCode

sort the intervals by their start value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals,(a,b)->a[0]-b[0]);
LinkedList<int[]> dq=new LinkedList<>();
for(int[] interval:intervals){
if(dq.size()==0||interval[0]>dq.peekLast()[1]) dq.addLast(interval);
else{
dq.getLast()[1]=Math.max(dq.peekLast()[1],interval[1]);
}
}
int[][] res=new int[dq.size()][2];
int i=0;
for(int[] interval:dq) res[i++]=interval;
return res;
}
}

<1…484950…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%