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 ...
随机推荐
- Discuz! X3.1去除内置门户导航/portal.php尾巴的方法
方法: 打开文件 /source/admincp/admincp_domain.php 查找 [php] if(!empty($domain) && in_array($domain, ...
- UML概述(转载)
UML是一种标准语言,用于指定,可视化,构造和文档的软件系统. UML是OMG在1997年1月提出了创建由对象管理组织(OMG)和UML1.0规范草案. OMG不断努力,使一个真正的行业标准. UML ...
- 链表(c语言实现)--------------小练习
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 #d ...
- 《Thinking in C++》学习笔记(一)【第二章】
第二章 对象的创建与使用 2.1语言的翻译过程 翻译器分为两类:解释器(interpreter)和编译器(compiler). 2.1.1解释器 解释器将源代码转化成一些动作(它可由许多机器指令组成) ...
- .htaccess的基本作用及相关语法介绍
.htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. .htaccess主要的作用有:URL重写.自定义错误页面.MIME类型配置以及访问权限控制等.主要体现在伪静态的应 ...
- ubuntu为Python添加默认搜索路径
我们在自己写python模块的时候,怎么样把自己写的模块加入到python默认就有的搜索路径中呢?不要每次非得import sys; sys.path.append(‘/home/uestc/rese ...
- PHP 反射机制Reflection
简介 PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类.方法.属性.参数等的详细信息,包括注释. class Reflection { } interface R ...
- 什么是A股、B股、H股、蓝筹股、红筹股
A股 A股的正式名称是人民币普通股票.它是由我同境内的公司发行,供境内机构.组织或个人(不含台.港.澳投资者)以人民币认购和交易的普通股股票,我国A股股票市场经过几年快速发展,已经初具规模. B股 B ...
- iOS开发cell--滑动手势显示按钮
// 主要代码 #warning iOS8 - #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮 - (NSArray *)tableView:(UITableView *)ta ...
- Java的Reference感觉很象C++的指针,但是区别是本质的
Java的Reference感觉很象C++的指针,但是区别是本质的 他们相同之处在于都是含有一个地址,但是在Java中你无法对这个地址进行任何数学运算,并且这个地址你不知道,是Java Runtime ...