non-overlapping-intervals
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的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- leetcode56. Merge Intervals
题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...
- Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【Leetcode】【Hard】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Java for LeetCode 056 Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode]题解(python):056-Merge Intervals
题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...
随机推荐
- (链接保存)CentOS 6.6下yum快速升级内核
(因Docker建议3.8以上kernel版本)这个方法升级很方便,保存链接备用: http://www.dadclab.com/archives/5340.jiecao Docker Engine安 ...
- asp.net 处理流程
原文:http://www.cnblogs.com/wupeiqi/archive/2013/03/03/2941295.html 工作进程: 在iis中,工作进程(w3wp.exe)运行着asp.n ...
- POJ 1742
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 27580 Accepted: 9335 Descriptio ...
- login shell 和 non-login shell 的区别
介绍之前先思考一个问题:有时我们通过su命令切换用户后,却发现并没有进入该用户的shell环境.这是为什么? login shell:取得bash时需要完整的登录流程.就是说通过输入账号和密码登录系统 ...
- To Use Ubuntuubunt
1.rename PC; modify the /etc/hostname. 2.Use root account; 1.set the password for root account: sudo ...
- root 授权
错误:The user specified as a definer ('root'@'%') does not exist 解决: grant all privileges on *.* to ro ...
- DMS平台从.NET 1.1升级到.NET 4.0的升级步骤
1)复制新增的项目到4.0平台解决方案对应目录,添加到到解决方案中:2)合并公共文件(比如修改了FormMain主界面.基础类库.售后界面的修改)3)控件的修订(Dev少数属性可能需要手工调整为新的方 ...
- UML类图、接口、包、关系
一.类图:允许我们去标记静态内容及类之间的关系. 类的基本表示法: 名称 属性(类型,可见性) 方法(参数,返回值) tip: 显示可见性:Options->Show Visibility 显 ...
- MongoDB 管理工具:Robomongo
http://www.open-open.com/lib/view/open1383029577546.html
- Java 获取本机IP
import java.io.*; import java.util.*; import java.net.*; public class GetIP { public static void mai ...