My 13ms C++


  • 0
    C

    /**

    • Definition for an interval.
    • struct Interval {
    • int start;
      
    • int end;
      
    • Interval() : start(0), end(0) {}
      
    • Interval(int s, int e) : start(s), end(e) {}
      
    • };
      */
      #include <algorithm>
      bool com(Interval a,Interval b){
      return a.start<b.start;
      }
      class Solution {
      public:
      vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
      vector<Interval> result;
      int len=intervals.size();
      Interval newIn;
      newIn.start=newInterval.start;
      newIn.end=newInterval.end;
      bool search=false;
      if(len==0) {result.push_back(newInterval);return result;}
      for(int i=0;i<len;i++){
      if(intervals[i].end<newInterval.start||intervals[i].start>newInterval.end){
      result.push_back(intervals[i]);
      }
      else{
      if(intervals[i].start<newInterval.start){
      newIn.start=intervals[i].start;
      search=true;
      if(intervals[i].end>newInterval.end){
      newIn.end=intervals[i].end;
      }
      }
      else{
      if(intervals[i].end>newInterval.end){
      newIn.end=intervals[i].end;
      }
      }
      }
      }
      result.push_back(newIn);
      sort(result.begin(),result.end(),com);
      return result;
      }
      };

Log in to reply
 

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