当前位置:网站首页>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).
};边栏推荐
- One interview question every day to talk about the process of TCP connection and disconnection
- Singapore parent-child tour, these popular attractions must be arranged
- Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
- svg和canvas的区别
- Collective system
- What to do when the alicloud SSL certificate expires
- Unity/ue reads OPC UA and OPC Da data (UE4)
- JPA composite primary key usage
- Redis implements SMS login function (II) redis implements login function
- One interview question a day the difference between B tree and b+ tree
猜你喜欢

Unity packaging failure solution

Unity3d realizes Google Digital Earth

Why does the computer have no network after win10 is turned on?
Sourcetree usage

Oracle-数据的基本操作

Introduction to some representations, neighbors and degrees of Graphs

Deeply understand the function calling process of C language

Ripple effect of mouse click (unity & shader)

Foreign SSL certificate

redis集群概念
随机推荐
[UAV] kinematic analysis from single propeller to four rotor UAV
力扣27. 移除元素
Wildcard SSL certificate issuing time
深度学习------不同方法实现Inception-10
How to renew an SSL certificate
Singapore must visit these scenic spots during the Spring Festival
Unity Logitech steering wheel access
Thread safety and processing caused by multithreading
Unity/ue reads OPC UA and OPC Da data (UE4)
Autowired注解警告的解决办法
Harbor API 2.0 query
Redis implements SMS login function (I) traditional session login
Solution to 293 problems in the week of Li Kou
Webots notes day 2
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
One interview question a day the difference between B tree and b+ tree
Using the command line to convert JSON to dart file in fluent
Malignant bug: 1252 of unit MySQL export
Unity3d lookat parameter description
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?