首页 > 其他 > 详细

252 Meeting Rooms

时间:2017-08-13 18:34:11      阅读:39      评论:0      收藏:0      [点我收藏+]

标签:pid   itl   china   syntax   ret   http   ||   -i   arrays   

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

/**
 * 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==null || intervals.length==0 || intervals.length==1) return true;
        Comparator<Interval> comp = new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start;
            }
        };
        
        Arrays.sort(intervals, comp);
        Interval pre = intervals[0];
        for (int i=1; i<intervals.length; i++) {
            Interval cur = intervals[i];
            if (cur.start < pre.end) return false;
            pre = cur;
        }
        return true;
    }
}

  

public static <T> void sort(T[] a,
                            Comparator<? super T> c)
根据指定比较器产生的顺序对指定对象数组进行排序。

252 Meeting Rooms

标签:pid   itl   china   syntax   ret   http   ||   -i   arrays   

原文:http://www.cnblogs.com/apanda009/p/7354220.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号