码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Insert Interval

时间:2014-11-04 22:20:44      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   for   sp   div   

题目:给定一系列的区间,这些区间是不重合的,而且按每个区间的起始点排好序了。再来一个区间。怎么得到所有合并后的区间。

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

考虑周到就可以做了。我是这样想的:

1.如果给定要插入区间的开始比之前某些区间的结束还大,那之前那些区间就肯定是答案一部分。最后要判断是不是已经是结尾了,如果是,插入区间后就可以返回了。

2.再1之后就找了当前区间的结束比要插入区间的start大或者相等,这个时候肯定就存在区间合并了,那么我们先确定合并之后区间tmp的start。这个start应该是min(当前区间的start和要插入的start),在继续找到tmp的end就完成了重复区间的合并。

3.找end,一直往后找,知道找到一个end比要插入的end大,那么这个时候要看要插入的end是不是在找到的这个区间中,如在,那么tmp的end显然就是现在遍历到的end。否则tmp的end就是要出入的end。

4.记得找到end后要把剩下的区间(如果有的话)记录。

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval)
{
    vector<Interval> ans;
    vector<Interval>::iterator p = intervals.begin();
    Interval tmp;
    while (p != intervals.end() && (*p).end < newInterval.start)
        ans.push_back(*p++); // 找到newInterval起点大的区间,如果有没有,就直接如下输出答案
    if (p == intervals.end()) 
    {
        ans.push_back(newInterval);
        return ans;
    }
    tmp = *p; // tmp主要用来记录跨越区间的起点和终点
    if((*p).start > newInterval.start) 
        tmp.start = newInterval.start; // 确定起点
    while(p!=intervals.end() && (*p).end < newInterval.end) 
        p++;
    if(p == intervals.end()) 
    {
        tmp.end = newInterval.end;
        ans.push_back(tmp);
        return ans;
    }
    if((*p).start <= newInterval.end) // 如果end在区间内,那么确定tmp的end后把剩下的区间也记录到答案中输出
    {
        tmp.end = (*p).end; ans.push_back(tmp);
        while(++p != intervals.end()) ans.push_back(*p);
        return ans;
    }
    tmp.end = newInterval.end;ans.push_back(tmp); // end不在区间内,那end就是tmp的end了,再把剩下的区间记录
    while(p!=intervals.end()) ans.push_back(*p++);
    return ans;
}
};

这个大神的解法写的比较简洁。他的思路是最后找要插入的区间的end是否大于某个区间的start来判断,而我是用end。

直接贴过来吧

vector<Interval> insert(vector<Interval> &v, Interval nv)   
    {  
        vector<Interval> rs;  
        int i = 0;  
        for ( ; i < v.size() && v[i].end < nv.start; i++)  
        {  
            rs.push_back(v[i]);  
        }  
        if (i == v.size())  
        {  
            rs.push_back(nv);  
            return rs;  
        }  
        nv.start = min(nv.start, v[i].start);  
  
        for ( ; i < v.size() && v[i].start <= nv.end; i++)  
        {  
            nv.end = max(nv.end, v[i].end);  
        }  
        rs.push_back(nv);  
  
        for ( ; i < v.size(); i++)  
        {  
            rs.push_back(v[i]);  
        }  
        return rs;  
    }  

 

leetcode Insert Interval

标签:style   blog   http   io   color   ar   for   sp   div   

原文地址:http://www.cnblogs.com/higerzhang/p/4074821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!