FTPrep, 57 Insert interval

来源:互联网 发布:大麦dm1016破解网络锁 编辑:程序博客网 时间:2024/05/18 15:08

这题还是有点意思,关键是能结合之前学过的数据结构,比如list,array,来实现相同的insert的功能,但是这里是给了一个新的object,要能活学活用。

如果输入是排序好的list,那么就是直接用,否则还是要像上一题 merge interval 一样先排序。

有了排序的输入,剩下的就是明确的思路了。本质上这个插入就是merge,要把将被 insert的interval 和已经存在的任何interval都merge 成一个interval,这样的话方法步骤就很明确了:

1,遍历input,如果有insert的头没有小于等于 i 的尾,那么就没有重合,所以就直接add to result

2,当1的中条件出现,那就等于是找到了merged 的 头部了,通过Math.max() 来得得到 interval的头部,头部就两种情况:

 insert:         (1)         ------------          (2)        ----------------------------

existing:        |      --------------              |                 -------------------


3,接着就是要找到 merged 的尾部,只要insert的尾巴大于等于 existing的头部,那就是更新 merged 的尾部。这样就完成merged 的构建,add to result

尾部的情况也是两种:

 insert:         (1)   ------------                        (2)         ----------------------------

existing:        |         --------------                   |                   -----------------

4,最后把剩余的item 挨个加入result

/** * 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; } * } *//*public class Interval{    int start;    int end;    Interval(){start=0; end=0;}    Interval(int start, int end){        this.start=start;        this.end=end;    }}*/class Solution {    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {        List<Interval> result = new ArrayList<Interval>();        if(intervals.size()==0){            result.add(newInterval);            return result;        }        int i=0;        while(i<intervals.size()&& intervals.get(i).end<newInterval.start){            result.add(intervals.get(i));            i++;        }        // the 1st step, chech the start of newInterval falls into where. As long as this point touches any end and leftforward of existing intervals, the newInterval and this interval should be merged. That is how we get the start of the merged parts. as following:             if(i<intervals.size())  newInterval.start= Math.min(newInterval.start, intervals.get(i).start);        //result.add(newInterval); // add the newInterval into the result, this line actually better be after the 2nd while loop, since then the end to the merged newInterval will be updated.        while(i<intervals.size() && intervals.get(i).start<=newInterval.end){            newInterval.end=Math.max(newInterval.end, intervals.get(i).end);            i++;        }        // 2nd while loop, need to update the end point of merged interval. As long as the end of newInterval touches the start of existing interval, the end point should be updated, using Math.max(). After this while loop, all the existing interval at the position that equal and bigger than i need to be added, that is what the following while loop does.        result.add(newInterval);        while(i<intervals.size()){            result.add(intervals.get(i));            i++;        }        return result;    }}


原创粉丝点击