LeetCode---56 学习人家的逆序思维

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

这道题呢,首先给出的测试数组并不是有序的,我因此首先想到的思路是:首先按照左端进行排序,这样就好解决问题,但是时间超时了。具体的代码如下:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public List sort(List intervals){
      int length = intervals.size();
        if(length<=0){
            return intervals;            
        }
        Interval temp = new Interval();
        
        for(int i = 0;i merge(List intervals) {
        List ret = new ArrayList();
        Interval temp = new Interval();
        int length = intervals.size();
        if(length<=0){
            return ret;
            
        }
        intervals = sort(intervals);
        
         temp.start = intervals.get(0).start;
         temp.end = intervals.get(0).end;
       
        
        for(int i=1; i temp.end){
                    ret.add(temp);
                    temp = new Interval();
                    temp.start = intervals.get(i).start;
                    temp.end = intervals.get(i).end;
                   
                }else{
                    temp.end=intervals.get(i).end;
                }
            }
        }
        ret.add(temp);

        return ret;
        
    }
}

于是我就在想,我用的排序时间过长??所以呢就想着如何不经过排序,这个过程让我很苦恼,但是还是写出来了,结果有点利用递归的感觉。设置标签,他是不是和他的子集相关,相关就合并,产生新的子集集合,这些子集集合是不是还可以合并,如果可以,则继续调用这个方法。直到集合不变,集合不变的标准就是数组的长度不变。于是,具体的代码如下:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
  
    public List merge(List intervals) {
        List ret = new ArrayList();
        int length = intervals.size();
        if(length<=0){
            return ret;            
        }
        int[] tag = new int[length];
        for(int i=0;i end || intervals.get(j).end < start){                    
                    
                    continue;
                }
                
                if(intervals.get(j).start <= start ){
                    start = intervals.get(j).start;
                    if(intervals.get(j).end >= end){
                        end=intervals.get(j).end;
                        tag[i]=1;
                    }
                    
                }else{
                    if( intervals.get(j).end > end){
                        end=intervals.get(j).end;                        
                    }
                }
                if(tag[i]==0){
                    tag[j]=1;
                }
                

            }
            if(tag[i]==0){
                Interval temp = new Interval();
                temp.start=start;
                temp.end=end;
                ret.add(temp);
                tag[i]=1;
            }
            
        }
        while(ret.size()!=intervals.size()){
            intervals=ret;
            ret=merge(intervals);
        }

        return ret;        
        
    }
}

但是呢,我提交答案后,我在自己编写的过程中就在想,我怎么不能从保存结果的方面考虑呢,但是当时还在走上一个思路中,就没想这个,提交后,发现真的这样想很给力的。代码比较简短。代码如下:来自大神的,

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public List merge(List intervals) {
        List res = new ArrayList();
        for(int i = 0; i < intervals.size(); i++){
            isoverlap(intervals.get(i), res);
        }
        return res;
        
    }
    
    public void isoverlap(Interval one, List res){
        int size = res.size() - 1;
        int min = one.start, max = one.end;
        for(int i = size; i >= 0; i--){
            Interval now = res.get(i);
            if(now.start > one.end || now.end < one.start){
                continue;
            }else{
                min = Math.min(one.start, Math.min(min, now.start));
                max = Math.max(one.end, Math.max(max, now.end));
                res.remove(i);
            }
        }
        res.add(new Interval(min, max));
            
    }
}
我的思维还是没想到这一步,很郁闷,哼,所以记录一下,希望以后学会这种思考方式。

你可能感兴趣的:(leetcode)