LeetCode_OJ【56】Merge Intervals
2016-02-19 19:53
363 查看
Given a collection of intervals, merge all overlapping intervals.
For example,
Given
return
这道题最好在57题做完了之后再做,leetcode上的这个顺序正好反过来了。
一开始直接上手这个题目的思路如下: 先对list中的所有interval按照start升序,end升序进行排序,排序可以借助Collections.sort()函数进行;然后将list中的每个interval的end属性和该interval后面的interval的start属性比较,如果前面interval的end大于后面的interval的start,则将后面的interval删去,前面的interval的end值取这两个interval的end值中的较大的那一个。
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals,
new Comparator<Interval>(){
public int compare(Interval a,Interval b){
return a.start == b.start ? a.end - b.end : a.start - b.start;
}
}
);
for(ListIterator<Interval> it = intervals.listIterator() ; it.hasNext(); ){
Interval pre = it.next();
if(it.hasNext()){
Interval p = it.next();
if(pre.end >= p.start){
pre.end = Math.max(pre.end,p.end);
it.remove();
}
it.previous();
}
}
return intervals;
}
这个方法比较好想,时间复杂度为O(nlogn),但是跑完所有的用力花了34ms,表现相当一般。
这个题目可以和57题目的方法结合起来使用,声明一个空的list,然后用57题的insert方法将intervals里面所有的interval都插入到list中。
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> res = new ArrayList<Interval>();
for(Interval interval : intervals){
insert(res,interval);
}
return res;
}
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if(intervals.size() == 0){
intervals.add(newInterval);
return intervals;
}
ListIterator<Interval> it = intervals.listIterator();
while(it.hasNext()){
Interval tmp = it.next();
if(tmp.start > newInterval.end){
it.previous();
it.add(newInterval);
return intervals;
}
else if(tmp.end < newInterval.start)
continue;
else {
newInterval.start = Math.min(newInterval.start, tmp.start);
newInterval.end = Math.max(newInterval.end, tmp.end);
it.remove();
}
}
it.add(newInterval);
return intervals;
}
}
For example,
Given
[1,3],[2,6],[8,10],[15,18],
return
[1,6],[8,10],[15,18].
这道题最好在57题做完了之后再做,leetcode上的这个顺序正好反过来了。
一开始直接上手这个题目的思路如下: 先对list中的所有interval按照start升序,end升序进行排序,排序可以借助Collections.sort()函数进行;然后将list中的每个interval的end属性和该interval后面的interval的start属性比较,如果前面interval的end大于后面的interval的start,则将后面的interval删去,前面的interval的end值取这两个interval的end值中的较大的那一个。
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals,
new Comparator<Interval>(){
public int compare(Interval a,Interval b){
return a.start == b.start ? a.end - b.end : a.start - b.start;
}
}
);
for(ListIterator<Interval> it = intervals.listIterator() ; it.hasNext(); ){
Interval pre = it.next();
if(it.hasNext()){
Interval p = it.next();
if(pre.end >= p.start){
pre.end = Math.max(pre.end,p.end);
it.remove();
}
it.previous();
}
}
return intervals;
}
这个方法比较好想,时间复杂度为O(nlogn),但是跑完所有的用力花了34ms,表现相当一般。
这个题目可以和57题目的方法结合起来使用,声明一个空的list,然后用57题的insert方法将intervals里面所有的interval都插入到list中。
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> res = new ArrayList<Interval>();
for(Interval interval : intervals){
insert(res,interval);
}
return res;
}
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if(intervals.size() == 0){
intervals.add(newInterval);
return intervals;
}
ListIterator<Interval> it = intervals.listIterator();
while(it.hasNext()){
Interval tmp = it.next();
if(tmp.start > newInterval.end){
it.previous();
it.add(newInterval);
return intervals;
}
else if(tmp.end < newInterval.start)
continue;
else {
newInterval.start = Math.min(newInterval.start, tmp.start);
newInterval.end = Math.max(newInterval.end, tmp.end);
it.remove();
}
}
it.add(newInterval);
return intervals;
}
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解