[lintcode]248. 统计比给定整数小的数的个数

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 07:58   96   0

链接:https://www.lintcode.com/zh-cn/problem/count-of-smaller-number/

给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。

 注意事项

在做此题前,最好先完成 线段树的构造 and 线段树查询 II 这两道题目。

样例

对于数组 [1,2,7,8,5] ,查询 [1,8,5],返回 [0,4,2]

class Solution {
public:
    /**
     * @param A: An integer array
     * @param queries: The query list
     * @return: The number of element in the array that are smaller that the given integer
     */
    vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
        // write your code here
        
        int sizeA=A.size(),sizeQ=queries.size();
        if(sizeA<=0 && sizeQ>0) return vector<int>(sizeQ,0);
        if(sizeA<=0 && sizeQ<=0) return vector<int>();
        
        vector<int> res;
        sort(A.begin(),A.end());
        for(auto target:queries)
        {
            int count=lower_bound(A.begin(),A.end(),target)-A.begin();
            res.push_back(count);
        }
        return res;
        
    }
};


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP