标签:public tin font false 交集 offer oom end com
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
.
此题重点是理解题意,会议时间不可以存在交集。
其次,了解排序,即数组有数组排序,Arrays.sort. Collections.sort. PriorityQueue()三种排序方式,都可以重写来实现。
代码如下:
/**
* 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; }
* }
*/
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if(intervals.length==0) return true;
PriorityQueue<Interval> q = new PriorityQueue<Interval>(intervals.length,new Comparator<Interval>(){
public int compare(Interval a,Interval b){
return a.start-b.start;
}
});
for(Interval i:intervals){
q.offer(i);
}
while(!q.isEmpty()){
Interval pre = q.poll();
if(!q.isEmpty()&&pre.end>q.peek().start) return false;
}
return true;
}
}
标签:public tin font false 交集 offer oom end com
原文地址:http://www.cnblogs.com/codeskiller/p/6362459.html