当前位置:网站首页>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).
};边栏推荐
- UE4 method of embedding web pages
- 【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
- Easyrecovery data recovery software recovers my photo and video data two years ago
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- Modbus protocol register
- Solution to the 292 week match of Li Kou
- Sectigo certificate
- Unity is associated with vs. there is a compiler problem when opening
- Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
- Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
猜你喜欢

MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值

Create a simple battle game with photon pun

Redis implements SMS login function (II) redis implements login function

UE4 method of embedding web pages
Sourcetree usage

Create transfer generation point

【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
![[fpga] implementation of IIC read / write EEPROM](/img/6a/36e9355058a90d98cffafcbaa1930b.png)
[fpga] implementation of IIC read / write EEPROM

【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey

Deeply understand the function calling process of C language
随机推荐
A virtual reality secret room escape adventure, let you see Technology Singapore
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Unity ontriggerenter does not call
Implementation of one interview question one distributed lock every day
Oculus quest2 development: (I) basic environment construction and guide package
Singapore parent-child tour, these popular attractions must be arranged
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
Unity Logitech steering wheel access
Unity lens making
JPA composite primary key usage
PBR material: basic principle and simple fabrication
HTC vive cosmos development - handle button event
LXC 和 LXD 容器总结
力扣589:N 叉树的前序遍历
Solution to the 292 week match of Li Kou
The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
Unity download and installation website
Unreal 4 learning notes - Animated Montage