Lintcode : 统计比给定整数小的数的个数
来源:互联网 发布:学党史知国情征文300字 编辑:程序博客网 时间:2024/05/12 14:52
统计比给定整数小的数的个数
给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。
您在真实的面试中是否遇到过这个题?
Yes
样例
对于数组 [1,2,7,8,5]
,查询 [1,8,5]
,返回 [0,4,2]
注意
在做此题前,最好先完成 线段树的构造 and 线段树查询 II 这两道题目。
挑战
标签 Expand 可否用一下三种方法完成以上题目。
仅用循环方法
分类搜索 和 二进制搜索
构建 线段树 和 搜索
相关题目 Expand
Timer Expand
解题思路:
直接使用2分搜索即可
public class Solution { /** * @param A: An integer array * @return: The number of element in the array that * are smaller that the given integer */ public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) { // write your code here Arrays.sort(A); ArrayList<Integer> res = new ArrayList<>(); for(int x:queries){ int tmp = lower_bound(x, 0, A.length-1, A); res.add(tmp); } return res; } public int lower_bound(int t,int start,int end ,int[] A){ while(start<=end){ int mid = (start+end)/2; if(A[mid]>=t){ end = mid -1; }else{ start = mid+1; } } return start; }}
0 0
- LintCode-统计比给定整数小的数的个数
- Lintcode : 统计比给定整数小的数的个数
- LintCode:统计比给定整数小的数的个数
- 统计比给定整数小的数的个数 -Lintcode
- lintcode(248)统计比给定整数小的数的个数
- LintCode:统计前面比自己小的数的个数
- 统计前面比自己小的数的个数-LintCode
- LintCode统计前面比自己小的数的个数(线段树)
- LINTCODE——统计前面比自己小的数的个数
- 统计前面比自己小的数的个数
- 给定一个正整数,找出一个数:与其二进制表示中1的个数相同,比该数小,而且最接近
- Arithmetic problem | 统计前面比自己小的数的个数
- 给定一个整数,求其二进制数中1的个数
- 统计前面比自己小的数
- 统计输入的整数区间内回文数的个数
- 找到比给定数大的数
- 给定一个正整数N,统计从1到N的整数中,出现“1”的个数(11这个数算出现两次)
- 统计给定一个整数以二进制表达时含有1的个数
- silverlight---------------------------
- charter 2-java对象和类
- JSP的内置对象及其用法
- MediaSession框架介绍
- 广告拍卖机制与点击率预估概述(二)
- Lintcode : 统计比给定整数小的数的个数
- 3. Longest Substring Without Repeating Characters
- Swift字符和字符串
- 在DLL中定义宏ELPP_THREAD_SAFE会导致EasyLogging++初始化死锁的问题
- 程序员简历制作建议
- SQL Server 通过配置数据库邮件实现发送邮件功能
- qt for android and opencv
- oracle中的采样数据
- Swift 常识篇