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的更多相关文章

  1. 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 * ...

  2. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  3. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...

  5. MySQL interval()函数

    INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...

  6. oracle11g interval(numtoyminterval())自动创建表分区

    Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...

  7. 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 ...

  8. Leetcode Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. Hdu 5489 合肥网络赛 1009 Removed Interval

    跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...

  10. [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 ...

随机推荐

  1. Load hlsl

    这个函数和sample差不多 不过没有samplestate和filter http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb5096 ...

  2. Nodejs Express 4.X 中文API 2--- Request篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...

  3. IOS Crash捕获

    IOS Crash ,就两种情况:一种是异常,另一种是中断[信号量]. #include <libkern/OSAtomic.h> #include <execinfo.h> ...

  4. 帝国cms数据还原后提示数据库表不存在怎么解决?

    下午,ytkah用帝国cms在wamp调试时发现了一个问题,还原备份好的数据后更新的页面提示数据库表不存在,查看了phpmyadmin分类的数据库表实际上是存在的,这个是怎么回事呢?重新搭建一个新站点 ...

  5. 疯狂java讲义——继承

    本文章只是记录我在学习疯狂java讲义里面,对之前java知识查缺补漏进行的总结. 方法重写 方法重写要遵循"两同两小一大"规则."两同"即方法名相同.形参列表 ...

  6. 关于JS中的constructor与prototype

    ======================================================================== 在学习JS的面向对象过程中,一直对constructo ...

  7. Ruby中的Profiling工具

    看看如何调试Ruby的性能问题 李哲 - APRIL 08, 2015 Ruby内置的profiler 内置的profiler实现的很简单,在ruby2.2中只有150行代码,大家可以看看它的实现pr ...

  8. 动态调用webservice 接口

    1.url:http://localhost:8002/名称.asmx(asmx结尾) 2.需要引用的命名空间:System.Web.Services 3.调用代码: public class Dyn ...

  9. Codeforces Round #258 (Div. 2)(A,B,C,D)

    题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...

  10. ExtJs之 Ext.JSON

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...