[我知道Arrays.sort()
的返回类型为void,并且toArray()
的返回类型为对象,所以我通过传入想要查看的对象进行了覆盖。
如何查看我的时间对象的内容?
抱歉,如果重复的话。
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
// Write your code here
if (intervals == null || intervals.size() == 0) {
return 0;
}
Arrays.sort(intervals.toArray(new Interval[intervals.size()]), new StartComparator());
int num = 1;
for(int i = 0; i < intervals.size() - 1 ; i++) {
System.out.print(intervals.get(i).toString());
if(intervals.get(i).end >= intervals.get(i + 1).end) {
num++;
}
}
return num;
}
private class StartComparator implements Comparator<Interval> {
@Override
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
}
}
假设您正在引用名为Interval
的自定义类,则可以简单地重写其toString()
方法以打印调试信息,例如实例变量start / end:
public String toString() {
return "Start : " + start + " -- End : " + end;
}