当前位置:网站首页>Force buckle 704 Binary search
Force buckle 704 Binary search
2022-06-30 04:56:00 【A wise man should not be bald】

class Solution {
public:
int search(vector<int>& nums, int target) {
// This dichotomy is to put target Put it in a left closed and right closed section , That is to say [left,right] in
// So make left The subscript position pointing to the first element of the array , Make right At the position of the subscript of the last element of the array .
int n = nums.size();
int left = 0;
int right = n-1;
int mid = 0; // The subscript of the intermediate element , It's defined in while Outside the loop is to avoid frequent creation , Because in while Inside the loop , The beginning of each cycle should be determined mid Size .
//<= Because left = right It makes sense .
while(left <= right){
// amount to mid = (left + right) / 2, Try not to write like this , Because when left and right When the value is large , Adding may cause data overflow .
mid = (right - left) /2 + left;
// If at present mid The element value at is exactly equal to target Words , Go straight back to mid
if(nums[mid] == target){
return mid;
}
// If at present mid The element value at is greater than target Words , Then it means that the target value to be found is mid The left side of the , So will right-1
else if(nums[mid] > target){
right = mid - 1;
continue;
}
// If at present mid Element value at is less than target Words , Then it means that the target value to be found is mid The right side of the , So will left-1
else{
left = mid + 1;
continue;
}
}
// This situation indicates that the target value is not nums Array , So back -1
return -1;
}
// Time complexity :O(logn), among n Is the length of the array .
// Spatial complexity :O(1).
};边栏推荐
- Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
- How does unity use mapbox to implement real maps in games?
- Static keyword
- 【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
- Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
- One interview question every day to talk about the process of TCP connection and disconnection
- Webots notes day 2
- 图的一些表示方式、邻居和度的介绍
- Some books you should not miss when you are new to the workplace
- Singapore parent-child tour, these popular attractions must be arranged
猜你喜欢

Photon pun refresh hall room list

Lambda&Stream

A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river

力扣209. 长度最小的子数组

Collective system

力扣27. 移除元素
![[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example](/img/d7/7bf43437edb87b69cdc5ae858f44e1.jpg)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example

Beanfactory creation process

EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据

Software digital signature certificate
随机推荐
What is multimodal interaction?
Using the command line to convert JSON to dart file in fluent
Create a simple battle game with photon pun
Keywords implements and @override
Check London attractions suitable for parents and children in winter vacation
力扣59. 螺旋矩阵 II
力扣周赛293题解
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
One interview question every day to talk about the process of TCP connection and disconnection
svg和canvas的区别
Yolov5 torch installation
Recommended cultural landmarks of these tourist attractions in Bangkok
pycharm 数据库工具
This connection is not a private connection this website may be pretending to steal your personal or financial information
0 basic unity course. Bricklaying
PBR material: basic principle and simple fabrication
為什麼win10開熱點後電腦沒有網絡?
Unity is associated with vs. there is a compiler problem when opening
Easyrecovery data recovery software recovers my photo and video data two years ago
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok