leetcode_question_57 Insert Interval

來源:互聯網
上載者:User

, insert and merge  in as .

, insert and merge  in as .

 overlaps with .

/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<Interval>::iterator itstart=intervals.begin(); for(; itstart!=intervals.end(); ++itstart)if((*itstart).start <= newInterval.start && newInterval.start <= (*itstart).end)break;vector<Interval>::iterator itend=intervals.begin();for(; itend!=intervals.end(); ++itend) if((*itend).start <= newInterval.end && newInterval.end <= (*itend).end)break;if(itstart!=intervals.end() && itend!=intervals.end()){if(itstart != itend){(*itstart).end = (*itend).end; int tmp = (*itstart).end; itstart++;while(itstart != intervals.end()) if((*itstart).start <= tmp) itstart = intervals.erase(itstart); else break;}return intervals;}else if(itstart==intervals.end() && itend!=intervals.end()){(*itend).start = newInterval.start; int tmp1 = (*itend).start; int tmp2 = (*itend).end; itstart = intervals.begin(); while((*itstart).start != tmp1 && (*itstart).end != tmp2) if((*itstart).start >= tmp1 && (*itstart).end <= tmp2) itstart = intervals.erase(itstart); else ++itstart;return intervals;}else if(itstart!=intervals.end() && itend==intervals.end()){(*itstart).end = newInterval.end; int tmp = newInterval.end; ++itstart;while(itstart != intervals.end()) if((*itstart).start < tmp) itstart = intervals.erase(itstart); else break;return intervals;}else{vector<Interval>::iterator it=intervals.begin();for(; it!=intervals.end(); ++it)if((*it).start > newInterval.start)break;//--it;vector<Interval>::iterator it1 = intervals.insert(it,newInterval); while(it1 != intervals.end()) { if((*it1).end < newInterval.end) it1 = intervals.erase(it1); else it1++; }return intervals;} }};


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.