find-right-interval
https://leetcode.com/problems/find-right-interval/
Java里面TreeMap或者TreeSet有类似C++的lower_bound或者upper_bound的函数:floor(取出不大于xx的)和ceiling(取出不小于xx的)
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 {
public int[] findRightInterval(Interval[] intervals) {
TreeMap<Integer, Integer> tmp = new TreeMap();
for (int i=0; i<intervals.length; i++) {
tmp.put(intervals[i].start, i);
}
int[] ret = new int[intervals.length];
for (int i=0; i<intervals.length; i++) {
Map.Entry<Integer, Integer> item = tmp.ceilingEntry(intervals[i].end);
if (item != null) {
ret[i] = item.getValue();
}
else {
ret[i] = -1;
}
}
return ret;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello!");
Solution solution = new Solution();
Interval[] intervals = new Interval[3];
intervals[0] = new Interval(3, 4);
intervals[1] = new Interval(2, 3);
intervals[2] = new Interval(1, 2);
int[] ret = solution.findRightInterval(intervals );
System.out.printf("Get ret: %d\n", ret.length);
for (int i=0; i<ret.length; i++) {
System.out.printf("%d,", ret[i]);
}
System.out.println();
}
}
find-right-interval的更多相关文章
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- MySQL interval()函数
INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...
- oracle11g interval(numtoyminterval())自动创建表分区
Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...
- maven执行报错resolution will not be reattempted until the update interval of nexus h
maven在执行过程中抛错: 引用 ... was cached in the local repository, resolution will not be reattempted until t ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Hdu 5489 合肥网络赛 1009 Removed Interval
跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...
- [LeetCode]436 Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
随机推荐
- Spring MVC学习问题记录
自2015年3月11日开始进行记录 day01 2015.03.11 问题1:Line 1 in XML document from URL is invalid; 今天出现了Content is n ...
- shell脚本 -d 是目录文件,那么-e,-f分别是什么?还有"! -e"这又是什么意思呢?
-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L filen ...
- Java 8怎么了:局部套用vs闭包
[编者按]本文作者为专注于自然语言处理多年的 Pierre-Yves Saumont,Pierre-Yves 著有30多本主讲 Java 软件开发的书籍,自2008开始供职于 Alcatel-Luce ...
- jquey ajax 无刷新提交form
http://bbs.csdn.net/topics/380237868 $.ajax({ type: "POST", url:ajaxCallUrl, data:$('#your ...
- Linux的别名使用
直接定义别名 编辑当前用户下的.bashrc 文件: vim ~/.bashrc 添加别名为 lmysql 的命令语句 : alias lmysql='mysql -uroot -p -Dtest ...
- spring 主题使用详解[转]
在common_include_v2.jsp文件中,spring主题的使用: <link href="${staticPath }/<spring:theme code='sty ...
- java传输json数据用md5加密过程
1.加密过程:客户端传输数据,包含两部分,一部分原始数据,一部分签名.签名就是对原始数据MD5加密后的字节序列.而原始数据就是普通的string字符串. 2.服务器端呢:将收到的原始数据,进行MD5加 ...
- 一个简单的ObjC和JavaScript交互工具
https://github.com/changjianfeishui/XBWebBridge ObjectiveC与Js交互是常见的需求,可对于新手或者所谓的高手而言,其实并不是那么简单明了.这里只 ...
- Emmet语法介绍
例子: (div+p#test>span.test2.test3)*5+p[name="hello"]>div.test4^a*5 <div></di ...
- Ubuntu rsync同步
>服务器端:Ubuntu 9.10 - 192.168.1.3客户端:Ubuntu 10.04 - 192.168.1.73 我们先来设置一下服务器端的配置 1.ubuntu系统安装完之后,rs ...