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 ...
随机推荐
- 宠物AI(个人觉得有问题)
//宠物AI流程 .刚开始添加宠物 Monster被动State:EnumMonster被NULL MonsterStat:EnumPet跟随 .附近有怪物就改成攻击模式(相关的收尾工作) void ...
- CSS Hack大全-可区分出IE6-IE10、FireFox、Chrome、Opera
今天把一些常用的CSS Hack整理了一下,包括常用的IE hack以及火狐.Chrome.Opera浏览器的Hack,并把这些CSS Hack综合的一起,写了一个小的浏览器测试器.如图所示: 下面就 ...
- 定位position详解:relative与absolute
定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在的位置上.然后通过设 ...
- 表单很多数据项录入的时候,提交controller发生异常,数据回显。
1.添加的情况(Model传递Form Data) request.getSession().setAttribute("car", car); //抛出异常的时候,数据回显. 2 ...
- vs2008 wince 通过字符串对控件操作
例如:我们知道控件名为"textbox1"需要对textbox1进行赋值:通常我们只要textbox1.text = "你好";即可 此处我们是通过" ...
- JSP 页面打印
<HTML><HEAD><TITLE>javascript打印-打印页面设置-打印预览代码</TITLE> <META http-equiv=Co ...
- 分布式数据存储 - MySQL双主复制
上篇文章<分布式数据存储 - MySQL主从复制>,我们说到MySQL主从复制很好的保障了从库,读的高可用性.so,问题来了: 1.针对主库,写的高可用性又是如何做到高可用性? 2.如果需 ...
- iOS第三方地图-百度地图定位的封装
// // BaiduMapTools.h // baidumapTest // // Created by apple on 15/8/26. // Copyright (c) 2015年 tqh. ...
- 简约的单页应用引擎:sonnyJS
点这里 SonnyJS是一个简约的单页应用引擎和HTML预处理器,旨在帮助开发人员和设计人员创建难以置信的强大和快速的单页网站. 主要特性: 模板嵌套,模板继承 动态同步模板路由(非Ajax) 跨窗口 ...
- line-height 与垂直居中!
在此之前,对于line-height 与垂直居中的问题,经常碰到. 比如,图片与span在同一个box中的时候,竟然会各种偏移.要想达到理想的效果真的是各种难. 有时间,决定认真的啃一啃. 一 lin ...