public class Solution { public static ArrayList<Interval> merge(ArrayList<Interval> intervals) { ArrayList<Interval> result = new ArrayList<Interval>(); if(intervals == null || intervals.size()==0) return result; Collections.sort(intervals,new IntervalComparator()); Interval prev = intervals.get(0); for(int i=1;i<intervals.size();i++){ Interval curr = intervals.get(i); if(prev.end>=curr.start){ Interval temp =new Interval(prev.start,Math.max(prev.end,curr.end)); prev = temp; }else{ result.add(prev); prev = curr; } } result.add(prev); return result; } static class IntervalComparator implements Comparator<Interval>{ public int compare(Interval s1,Interval s2){ return (int) ((s1.start-s2.start)/24/3600/1000); } } }
public class Interval { long start; long end; public long getStart() { return start; } public void setStart(long start) { this.start = start; } public long getEnd() { return end; } public void setEnd(long end) { this.end = end; } public Interval(long start, long end) { super(); this.start = start; this.end = end; } public Interval() { super(); } }
int workDay = 0; for (Interval interval : list) { workDay += (interval.getEnd() - interval.getStart())/24/3600/1000; }
善于将复杂问题简单化