zoukankan      html  css  js  c++  java
  • LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

    感觉有一点进步了,但是思路还是不够犀利。

    /**
     * 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> merge(vector<Interval>& intervals) {
            vector<pair<int,int>> r;
            for(int i=0;i<intervals.size();i++)
                r.push_back(make_pair(intervals[i].start,intervals[i].end));
            sort(r.begin(),r.end());
            int index=0;
            vector<Interval> res;
            while(index<r.size()){
                int start=r[index].first;
                int end=r[index].second;
                if(start == INT_MAX)
                {
                    index++;
                    continue;
                }
                for(int i=index+1;i<r.size();i++)
                {
                    if(r[i].first == end){
                        end=r[i].second;
                        r[i].first=INT_MAX,r[i].second=INT_MAX;
                        
                    }
                    
                }
                while(r[index+1].first >= start && r[index+1].second<=end && r[index+1].second>=start&&r[index+1].second<=end)
                {
                    index++;
                }
                while(end>=r[index+1].first && end<=r[index+1].second && index+1 <=r.size())
                {
                    index++;
                    end=r[index].second;
                }
                res.push_back(Interval(start,end));
                index++;
            }
            return res;
        }
    };
    

      本地测试正确,不知道哪里有问题

    #include"stdafx.h"
    #include<iostream>
    #include<vector>
    #include<set>
    #include<math.h>
    #include<map>
    #include<sstream>
    #include<algorithm>
    #include<stack>
    #include<queue>
    using namespace std;
     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> merge(vector<Interval>& intervals) {
    		auto next = intervals.begin();
    		auto pre = next++;
    		while (next != intervals.end()) {
    			if (pre->end >= next->start) {
    				pre->end = next->end;
    				next = intervals.erase(next);
    			}
    			else {
    				pre++;
    				next++;
    			}
    		}
    		return intervals;
    	}
    };
    
    int main()
    {
    	vector<Interval> coll;
    	Interval a =  Interval(1, 3);
    	Interval b = Interval(2, 6);
    	Interval c = Interval(8, 10);
    	Interval d = Interval(15, 18);
    	coll.push_back(a);
    	coll.push_back(b);
    	coll.push_back(c);
    	coll.push_back(d);
    //	coll.erase(coll.begin());
    	Solution s;
    	s.merge(coll);
    	for (auto i : coll)
    		cout << i.start << " " << i.end << endl;
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    内敛函数
    墓碑文件
    java默认继承
    Question2Answer初体验
    yii 事物
    情商
    jsonp跨域
    CDbConnectionExt.php 23.2实现数据库的主从分离,该类会维护多个数据库的配置:一个主数据库配置,多个从数据库的配置
    图片服务器规划漫谈
    c语言中返回结构体的函数(结构体可以被赋值,类型相同的结构体可以相互赋值)
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5013454.html
Copyright © 2011-2022 走看看