zoukankan      html  css  js  c++  java
  • LeetCode() Insert Interval

    auto compare = [] (const Interval &intv1, const Interval &intv2)
                              { return intv1.end < intv2.start; };
            auto range = equal_range(intervals.begin(), intervals.end(), newInterval, compare);
            auto itr1 = range.first, itr2 = range.second;
            if (itr1 == itr2) {
                intervals.insert(itr1, newInterval);
            } else {
                itr2--;
                itr2->start = min(newInterval.start, itr1->start);
                itr2->end = max(newInterval.end, itr2->end);
                intervals.erase(itr1, itr2);
            }
            return intervals;
    

      这道题我本科的时候肯定做过,现如今真的变笨了

    int left=0,right=intervals.size();
            for(int i=0;i<intervals.size();i++)
                if(newInterval.start>intervals[i].end)
                    left=i+1;
            for(int i=intervals.size()-1;i>=0;i--)
                if(newInterval.end<intervals[i].start)
                    right=i;   //the right is the one after the last one
    
            //if the new interval is in the head,then insert as new head
            if(right==0)
            {
                intervals.insert(intervals.begin(),newInterval);
                return intervals;
            }
            //if the new interval is in the tail,then insert as new tail 
            if(left==intervals.size())
            {
                intervals.insert(intervals.end(),newInterval);
                return intervals;
            }
            //construct the newinterval
            newInterval.start=min(newInterval.start,intervals[left].start);
            newInterval.end=max(newInterval.end,intervals[right-1].end);
    
            //firt erase the old intervals,then insert one new interval
            intervals.erase(intervals.begin()+left,intervals.begin()+right);
            intervals.insert(intervals.begin()+left,newInterval);
    
            return intervals;
    

      

  • 相关阅读:
    暂存未看的网址
    学习springboot+shiro的实际应用demo
    mapper.xml 的配置
    使用idea搭建Spring boot+jsp的简单web项目
    一文读懂SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系
    .net core mvc 类库读取配置文件
    .net Core 2.0应用程序发布到IIS上注意事项
    Dapper扩展
    C# 请求Https
    js 记录
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5018961.html
Copyright © 2011-2022 走看看