题目链接:https://leetcode.com/problems/meeting-rooms/
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...]
(si < ei), determine if a person could attend all meetings.
For example,
Given [[0, 30],[5, 10],[15, 20]]
,
return false
.
思路:非常简单的查区间是否有重合的题目,先按照start排个序,sweep line
代码如下:
/** * 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: bool canAttendMeetings(vector<Interval>& intervals) { if(intervals.size() == 0) return true; sort(intervals.begin(), intervals.end(), [](Interval a, Interval b){ return a.start < b.start;}); int end = intervals[0].end; for(int i = 1; i< intervals.size(); i++) { if(intervals[i].start < end) return false; end = intervals[i].end; } return true; } };