首页 > 其他 > 详细

Leetcode: Meeting Rooms

时间:2015-12-22 09:00:11      阅读:478      评论:0      收藏:0      [点我收藏+]
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.

Implement a Comparator<Interval>

Syntax: don‘t forget the public sign when defining a function

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public boolean canAttendMeetings(Interval[] intervals) {
12         if (intervals==null || intervals.length==0 || intervals.length==1) return true;
13         Comparator<Interval> comp = new Comparator<Interval>() {
14             public int compare(Interval i1, Interval i2) {
15                 return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start;
16             }
17         };
18         
19         Arrays.sort(intervals, comp);
20         Interval pre = intervals[0];
21         for (int i=1; i<intervals.length; i++) {
22             Interval cur = intervals[i];
23             if (cur.start < pre.end) return false;
24             pre = cur;
25         }
26         return true;
27     }
28 }

 

Leetcode: Meeting Rooms

原文:http://www.cnblogs.com/EdwardLiu/p/5065525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!