simple and easy to understand


  • 0
    W
    bool overlap(Interval& int1, Interval& int2) {
        if (int1.end < int2.start || int2.end < int1.start)
            return false;
        int1.start = int1.start < int2.start? int1.start: int2.start;
        int1.end = int1.end > int2.end? int1.end : int2.end;
        return true;
    }
    
    vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
        vector<Interval> ret;
        Interval curr;
    
        bool flag = false;
        for (auto it: intervals) {
            if (flag) {
                ret.push_back(it);
                continue;
            }
            if (overlap(newInterval, it)) {
                continue;
            }
            if (it.start < newInterval.start)
                ret.push_back(it);
            else {
                ret.push_back(newInterval);
                ret.push_back(it);
                flag = true;
            }
        }
        if (!flag) {
            ret.push_back(newInterval);
        }
        return ret;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.