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<intervals.length; i++) {
if (intervals[i].start >= 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<List<Integer>> 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(); }
}

non-overlapping-intervals的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  2. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  5. leetcode56. Merge Intervals

    题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...

  6. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  8. 【Leetcode】【Hard】Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  9. Java for LeetCode 056 Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  10. [LeetCode]题解(python):056-Merge Intervals

    题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...

随机推荐

  1. while小问题

    while(!m_SMque.pop(data)); 看到这个有点忘了,如果pop返回false会一直执行pop,其实这个执行的是空语句,而while每次执行都需要判断条件,所以如果pop返回fals ...

  2. NDK: unable to watch local variables after using GCC4.8

    the problem definitly apears after changing toolchain from gcc 4.6 to gcc 4.8. here's a solution wit ...

  3. 运行时修改TimerTask的执行周期

    java.util.TimerTask类的执行周期period变量的声明如下: /** * Period in milliseconds for repeating tasks. A positive ...

  4. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  5. POJ 1279 Art Gallery(半平面交求多边形核的面积)

    题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...

  6. java对象群体的组织:Enumeration及Iterator类

    在一般情况下,遍历集合类会使用一下方式: for(int i=0;i<v.size();i++)< p=""> Customer c=(Custormer)v.g ...

  7. html + css + js注释规范

    添加注释到代码中,是一个很好的习惯,而且极大的提高了代码的可读性 1.HTML <!--commentContent--> 2.CSS //commentContent /*comment ...

  8. [转载]Jmeter那点事·ForEach和If控制器

    如果我们要实现一个循环,如果城市是北京,则返回首都:否则,返回城市.   一.新建用户自定义变量 添加-配置元件-用户自定义变量, 定义变量注意命名格式:变量名 加 下划线 加 数字(从1开始计数) ...

  9. 【hdu2815-Mod Tree】高次同余方程-拓展BadyStepGaintStep

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 题意:裸题... 关于拓展BSGS的详细解释我写了一篇博文:http://www.cnblogs.com/ ...

  10. 继电器Relay:ZZR08

    继电器常识: 继电器有三个接线柱:常开(NO),常闭(NC),接地(C) 如果连接的时间长,偶尔需要断电, 那么接NC 和 C, 这样继电器set on 时为断电.除此之外,继电器还可以控制按键,以及 ...