https://leetcode.com/problems/non-overlapping-intervals/
其中还用到了Java的Comparator接口和其中的compare方法。
package com.company;import java.util.*;class Interval { int start; int end; Interval() { start = 0; end = 0; } Interval(int s, int e) { start = s; end = e; }}class Solution { class IntervalComp implements Comparator { @Override public int compare(Object o1, Object o2) { Interval i1 = (Interval)o1; Interval i2 = (Interval)o2; return i1.start - i2.start; } } public int eraseOverlapIntervals(Interval[] intervals) { if (intervals.length == 0) { return 0; } Arrays.sort(intervals, new IntervalComp()); int ret = 0; int last = intervals[0].end; for (int i=1; i= last) { last = intervals[i].end; continue; } if (intervals[i].end >= last) { ret++; } else { ret++; last = intervals[i].end; } } return ret; }}public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!"); Solution solution = new Solution(); Interval[] it = new Interval[2]; it[0] = new Interval(1, 2); it[1] = new Interval(2, 3); int ret = solution.eraseOverlapIntervals(it); System.out.printf("Get ret: %d\n", ret); System.out.println(); /*Iterator
> iterator = ret.iterator(); while (iterator.hasNext()) { Iterator iter = iterator.next().iterator(); while (iter.hasNext()) { System.out.printf("%d,", iter.next()); } System.out.println(); }*/ System.out.println(); }}