当前位置:网站首页>[daily question on niuke.com] two point search
[daily question on niuke.com] two point search
2022-06-12 05:29:00 【cbdgz】
Title Description :
Given an ordered array of known length , Use binary search to find the first subscript greater than or equal to its value ( Subscript position from 1 Start )
Core code
int Bsearch(int v,vector<int>a,int n)
{
if(v>a[n-1])return n+1;
if(v<a[0])return 1;
int i=0,j=n-1,ans=n+1;
while(i<=j)
{
int mid=(j+i)/2;
if(v==a[mid])
{
ans=mid;
break;
}
if(v>a[mid])i=mid+1;
if(v<a[mid])j=mid-1;
}
if(ans==n+1)return i;
else{
int k=ans;
while(k>0&&a[k]==v)--k;
return k+2;
}
}
边栏推荐
- UBI details and JFFS2 square FS UBIFS
- Towards End-to-End Lane Detection: an Instance SegmentationApproach
- Design of a simple embedded web service application
- JS how to get the date
- 4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
- Codec of ASoC framework driven by alsa
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- How to clear floating, and how does it work?
- Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
- MySQL Linux Installation mysql-5.7.24
猜你喜欢

深入理解异步编程

Legal liabilities to be borne by the person in charge of the branch

16. sum of the nearest three numbers
![[cjson] precautions for root node](/img/6c/4462bc8126935f00a5c64f18de13ea.png)
[cjson] precautions for root node

16. 最接近的三数之和

Detailed usage of vim editor

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class

Is the individual industrial and commercial door a legal person enterprise

week7

WebRTC AEC 流程解析
随机推荐
什么是工程预付款
Esp32-who face detection
Stm32f4 ll library multi-channel ADC
Development of video preview for main interface of pupanvr-ui
Map coordinate conversion of Baidu map API
WebRTC AEC 流程解析
Platform of ASoC framework driven by alsa
Performance test - GTI application service performance monitoring platform
What is thinking
Deep understanding of asynchronous programming
44. a digit in a sequence of digits
49. ugly number
Introduction to redis cluster
59 - II. Maximum value of the queue
Automated testing - Po mode / log /allure/ continuous integration
How does WiFi 802.11 correspond to 802.3
[cjson] precautions for root node
Codec of ASoC framework driven by alsa
分公司负责人需要承担的法律责任
When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path