栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

NC37 合并区间(C++)- 中等、排序、数组

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NC37 合并区间(C++)- 中等、排序、数组

题目链接:https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a?tpId=196&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Fpage%3D2%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=&judgeStatus=&tags=&title=37&gioEnter=menu
题目如下:

class Solution {
public:
    static bool cmp(Interval &a,Interval &b){
        if(a.start==b.start) return a.end merge(vector &intervals) {
        vector res;
        if(intervals.size()==0) return res;
        else if(intervals.size()==1) {res.push_back(intervals[0]);return res;}
        sort(intervals.begin(),intervals.end(),cmp);//类型不同。要用cmp
        
        Interval temp;
        int l=intervals[0].start,r=intervals[0].end;
        for(int i=1;i
            if(r>=intervals[i].start){//需要合并
                r=max(r,intervals[i].end);
                
                //temp.start=l;temp.end=r;
                //res.push_back(temp);
                //上面两行不对,因为此时不确定后面一个区间是否要和当前这个区间合并
            }else {//先保存独立的区间,再更新左右指针
                temp.start=l;temp.end=r;
                res.push_back(temp); 
                
                l=intervals[i].start;
                r=intervals[i].end;      
            }
        }
        
        temp.start=l;temp.end=r;
        res.push_back(temp);
        
        return res;
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982857.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号