LeetCode 題解(254) : 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排序,然後比較前一個的end和後一個的start。
C++版:
/**
* 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) {
sort(intervals.begin(), intervals.end(), compare);
for(int i = 1; i < intervals.size(); i++) {
if(intervals[i - 1].end > intervals[i].start)
return false;
}
return true;
}
static bool compare(Interval a, Interval b) {
return a.start < b.start;
}
};
Java版:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
import java.util.Arrays;
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
Arrays.sort(intervals, new IntervalComparator());
for(int i = 1; i < intervals.length; i++) {
if(intervals[i - 1].end > intervals[i].start)
return false;
}
return true;
}
}
class IntervalComparator implements Comparator<Interval> {
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
}
Python版:
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
intervals.sort(lambda a, b : a.start - b.start)
for i in range(1, len(intervals)):
if intervals[i - 1].end > intervals[i].start:
return False
return True
相關文章
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- Meeting Rooms IIOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 252. Meeting RoomsOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- 253. Meeting Rooms IIOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- 【LeetCode(Java) - 254】因子的組合LeetCodeJava
- Leetcode 253: meeting roomLeetCodeOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- LeetCode-Best Meeting PointLeetCode
- [leetcode 題解] 849LeetCode
- Leetcode 全套題解LeetCode
- 「LeetCode」全部題解LeetCode
- leetcode題解(陣列問題)LeetCode陣列
- Leetcode題解1-50題LeetCode
- Leetcode 565 & 240 題解LeetCode