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

插入区间

时间:2017-05-25 15:41:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:++   插入   bsp   自己   over   log   ==   break   val   

以下是我自己的代码

    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        //write your code here
        vector<Interval> a;
        Interval b(0, 0);
        int x = 0;
        int y = 0;
        int m = 0;
        int n = intervals.size();
        for (size_t i = 0; i != n; i++) {
        // 左边数字的判断
            if (x == 0 && newInterval.start <= intervals[i].start) {
                b.start = newInterval.start;
                x = 1;
            }
            else if (x == 0 && newInterval.start <= intervals[i].end) {
                b.start = intervals[i].start;
                x = 1;
            }
            else if (x == 0){
                b.start = intervals[i].start;
            }
        // 下面是右边数字的判断
            if (y == 0 && newInterval.end < intervals[i].start) {
                b.end = newInterval.end;
                y = 1;
            }
            else if (y == 0 && newInterval.end == intervals[i].start) {
                b.end = intervals[i].end;
                y = 2;
            }
            else if (y == 0 && newInterval.end <= intervals[i].end) {
                b.end = intervals[i].end;
                y = 3;
            }
            else if (y == 0){
                b.end = intervals[i].end;
            }

            if ( m == 0 && (x != 0 && y != 0) ) {
                m++;
                a.push_back(b);
            }
            if (y == 0 && x == 0)
                a.push_back(b);
            if (x != 0 && y != 0) {
                if (y == 2 || y == 3) {
                    y = 1;
                }
                else
                    a.push_back(intervals[i]);
            }
        }
        if (x == 0 && y == 0) {
            a.push_back(newInterval);
        }

        if (x != 0 && y == 0) {
            b.end = newInterval.end;
            a.push_back(b);
        }
        return a;
    }

别人家的代码

 1 // Using Index
 2 class Solution {
 3 public:
 4     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
 5         vector<Interval> res = intervals;
 6         int i = 0, overlap = 0, n = res.size();
 7         while (i < n) {
 8             if (newInterval.end < res[i].start) break;  
 9             else if (newInterval.start > res[i].end) {} 
10             else {
11                 newInterval.start = min(newInterval.start, res[i].start);
12                 newInterval.end = max(newInterval.end, res[i].end);
13                 ++overlap;
14             }
15             ++i;
16         }
17         if (overlap > 0) res.erase(res.begin() + i - overlap, res.begin() + i);
18         res.insert(res.begin() + i - overlap, newInterval);
19         return res;
20     }
21 };

 

插入区间

标签:++   插入   bsp   自己   over   log   ==   break   val   

原文地址:http://www.cnblogs.com/Dbbf/p/6903860.html

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