1. 程式人生 > >leetcode 56:合併區間

leetcode 56:合併區間

void sort(std::vector<Interval> &a){
    Interval b(0,0);
    Interval temp(0,0);
    int t=0;
    for(int i=0;i<a.size();i++){
        b=a[i];
        t=0;
        for(int j=i+1;j<a.size();j++){
            if(a[j].start<b.start){
                b=a[j];
                t=j;
            }
        }
        if(t!=0){
            temp=a[i];
            a[i]=a[t];
            a[t]=temp;
        }
    }
}

std::vector<Interval> merge(std::vector<Interval>& intervals) {
     std::vector<Interval> result;
    if(intervals.size()==0)
        return result;
    if(intervals.size()==1)
        return intervals;
    sort(intervals);
    int i=1;
    Interval a=intervals[0];
    while(i!=intervals.size()){
        if(a.end<intervals[i].start) {
            result.push_back(a);
            a=intervals[i];
        }else if(a.end>=intervals[i].start&&a.end<intervals[i].end){
            a.end=intervals[i].end;
        }
        i++;
    }
    result.push_back(a);
    return result;
}