码迷,mamicode.com
首页 > 其他好文 > 详细

LC436. Find Right Interval

时间:2020-03-20 11:06:53      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:==   else   com   标准   tin   ++   color   code   hash   

技术图片

分析:求一个区间最邻近的右边的区间在数组中的索引位置,右侧区间头要大于等于左侧区间尾。用map存区间头对应的区间索引。

标准库有map自己的lower_bound函数,返回大于等于key的第一个值的iteraotr。找右侧最邻近区间就是找 lower_bound(intervals[i][1]) .

class Solution {
public:
    vector<int> findRightInterval(vector<vector<int>>& intervals) {
        map<int, int> hash;
        vector<int> res;
        int n = intervals.size();
        for (int i = 0; i < n; ++i) {
            hash[intervals[i][0]] = i;
        }
        for (auto& in : intervals) {
            auto it = hash.lower_bound(in[1]);
            if (it == hash.end()) res.push_back(-1);
            else res.push_back(it->second);
        }
        return res;
    }
};

 

LC436. Find Right Interval

标签:==   else   com   标准   tin   ++   color   code   hash   

原文地址:https://www.cnblogs.com/betaa/p/12529928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!