File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
solution/0056.Merge Intervals Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for an interval.
3
+ * public class Interval {
4
+ * int start;
5
+ * int end;
6
+ * Interval() { start = 0; end = 0; }
7
+ * Interval(int s, int e) { start = s; end = e; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public List <Interval > merge (List <Interval > intervals ) {
12
+ int n =intervals .size ();
13
+ int [] starts =new int [n ],ends =new int [n ];
14
+ for (int i =0 ;i <n ;i ++){
15
+ starts [i ]=intervals .get (i ).start ;
16
+ ends [i ]=intervals .get (i ).end ;
17
+ }
18
+ Arrays .sort (starts );
19
+ Arrays .sort (ends );
20
+ List <Interval > res = new ArrayList <>();
21
+ for (int i =0 ,j =0 ;i <n ;i ++){
22
+ if ((i == (n - 1 )) || (starts [i + 1 ] > ends [i ])){
23
+ res .add (new Interval (starts [j ],ends [i ]));
24
+ j =i +1 ;
25
+ }
26
+ }
27
+ return res ;
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments