Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcba57ce

Browse files
merge intervals
1 parent9a7bb96 commitcba57ce

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

‎HARD/src/hard/MergeIntervals.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
packagehard;
2+
importjava.util.*;
3+
importclasses.Interval;
4+
importutils.CommonUtils;
5+
6+
/**
7+
* Created by fishercoder1534 on 10/3/16.
8+
*/
9+
publicclassMergeIntervals {
10+
11+
/**Inspired by this post: https://discuss.leetcode.com/topic/4319/a-simple-java-solution
12+
* 1. Sort the intervals first, based on their starting point
13+
* 2. then compare the end point with next interval's start point, if they overlap, then update the end point to the longest one,
14+
* if they don't overlap, we add it into the result and continue the iteration.*/
15+
publicstaticList<Interval>merge(List<Interval>intervals) {
16+
if(intervals.size() <=1)returnintervals;
17+
18+
Collections.sort(intervals,newComparator<Interval>() {
19+
@Override
20+
publicintcompare(Intervalo1,Intervalo2) {
21+
returno1.start -o2.start;
22+
}
23+
});
24+
25+
List<Interval>result =newArrayList();
26+
for(inti =0;i <intervals.size();i++){
27+
intstart =intervals.get(i).start;
28+
intend =intervals.get(i).end;
29+
while(i <intervals.size() &&end >=intervals.get(i).start){
30+
end =Math.max(end,intervals.get(i).end);
31+
i++;
32+
}
33+
result.add(newInterval(start,end));
34+
i--;
35+
}
36+
returnresult;
37+
}
38+
39+
publicstaticvoidmain(String[]args){
40+
List<Interval>list =newArrayList<Interval>();
41+
// //test case 1:
42+
// list.add(new Interval(2,3));
43+
// list.add(new Interval(5,5));
44+
// list.add(new Interval(2,2));
45+
// list.add(new Interval(3,4));
46+
// list.add(new Interval(3,4));
47+
48+
//test case 2:
49+
list.add(newInterval(1,3));
50+
list.add(newInterval(2,6));
51+
list.add(newInterval(8,10));
52+
list.add(newInterval(15,18));
53+
CommonUtils.printList(merge(list));
54+
}
55+
56+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../../blob/master/MEDIUM/src/medium/CloneGraph.java)| O(n)|O(n) | Medium| HashMap, BFS
77
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../blob/master/MEDIUM/src/medium/DecodeWays.java)| O(n)|O(n) | Medium| DP
88
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../../blob/master/HARD/src/hard/MinimumWindowSubstring.java)|O(n)|O(k)|Hard|Two Pointers
9+
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/HARD/src/hard/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
910
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../../blob/master/HARD/src/hard/MergeKSortedList.java)|O(n*logk)|O(logk)|Hard|Heap
1011
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Solution](../../blob/master/MEDIUM/src/medium/LetterCombinationsofaPhoneNumber.java)|O(n*4^n)|O(n)|Medium|Backtracking
1112
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../../blob/master/HARD/src/hard/RegularExpressionMatching.java)|O(m*n)|O(m*n)|Hard|DP

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp