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();

    }
}
时间: 2024-10-14 01:49:02

find-right-interval的相关文章

JsSIP.UA.JsSIP 总是返回错误:422 Session Interval Too Small

在JsSIP 中 JsSIP.UA.call 总是 返回错误:422 Session Interval Too Small 关于错详情在这篇文章中解释的比较详尽:http://www.cnblogs.com/yoyotl/p/4980817.html 但是没有JsSIP的解决方法 具体的解决方法如下: JsSIP.js中的搜索 SESSION_EXPIRES: 把这个参数改大,  大于120就行了, (要是还不行,就继续加大吧^.^) SESSION_EXPIRES: 90 加大: SESSIO

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

AngularJS中$timeout和$interval的用法详解

1. 先将$interval,$timeout,作为参数注入到controller中,例如rds.controller('controllerCtrl', ['app', '$scope','$http','$routeParams','$filter','$location','$interval','$timeout', function (app, $scope,$http,$routeParams,$filter,$location,$interval,$timeout) {2.在需要用

LeetCode57 Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and mer

[LeetCode]题解(python):057-Insert Interval

题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Examp

[LeetCode][Java] Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and me

LeetCode --- 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

PLSQL_基础系列4_时间间隔INTERVAL

2014-12-08 BaoXinjian 一.摘要 INTERVAL数据类型用来存储两个时间戳之间的时间间隔. 可以指定years and months,或者days,hours,minuts,seconds之间的间隔. ORACLE支持两种INTEVAL类型,它们分别是YEAR TO MONTH和DAY TO SECOND. 每个类型都包含leading field和trailing field.主参数定义要被计算的date或者time,副参数定义最小增长量 二.语法 1. Oracle语法

Interval(南阳oj522)(树状数组)

Interval 时间限制:2000 ms  |  内存限制:65535 KB 难度:4 描述 There are n(1 <= n <= 100000) intervals [ai, bi] and m(1 <= m <= 100000) queries, -100000 <= ai <= bi <= 100000 are integers. Each query contains an integer xi(-100000 <= x <= 1000

每日算法之四十:Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge