当前位置:网站首页>力扣704. 二分查找
力扣704. 二分查找
2022-06-30 04:45:00 【智慧的人不要秃头】

class Solution {
public:
int search(vector<int>& nums, int target) {
//此种二分写法是将target放在一个左闭右闭的区间里,也就是[left,right]中
//所以令left指向数组的第一个元素的下标位置,令right在数组最后一个元素的下标的位置。
int n = nums.size();
int left = 0;
int right = n-1;
int mid = 0; //中间元素的下标,定义在while循环外面是为避免频繁的创建,因为在while循环里面,每一次循环的开始都要确定mid的大小。
//<=是因为left = right是有意义的。
while(left <= right){
//相当于mid = (left + right) / 2,应尽量不这样写,因为当left和right数值很大的时候,相加可能会造成数据的溢出。
mid = (right - left) /2 + left;
//如果当前mid处的元素值正好等于target的话,直接返回mid
if(nums[mid] == target){
return mid;
}
//如果当前mid处的元素值大于target的话,那么说明要找的目标值在mid的左侧,所以将right-1
else if(nums[mid] > target){
right = mid - 1;
continue;
}
//如果当前mid处的元素值小于target的话,那么说明要找的目标值在mid的右侧,所以将left-1
else{
left = mid + 1;
continue;
}
}
//此种情况说明目标值不在nums数组中,所以返回-1
return -1;
}
//时间复杂度:O(logn),其中 n 是数组的长度。
//空间复杂度:O(1)。
};边栏推荐
- Qos(Quality of Service)
- 【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc
- Dual domain SSL certificate
- Unity automatic pathfinding
- The difference between get and post requests
- Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
- Unreal 4 learning notes - Animated Montage
- One interview question and one joint index every day
- Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
- EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
猜你喜欢

Software digital signature certificate

Oculus quest2 development: (I) basic environment construction and guide package

【Paper】2017_ Distributed control for high-speed trains movements

Learning about signals

【Paper】2019_ Distributed Cooperative Control of a High-speed Train

Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge

Directory operations and virtual file systems

【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai

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

Unity is associated with vs. there is a compiler problem when opening
随机推荐
Draw on screen border in Commodore 64
Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Introduction to system programming
Learning about signals
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
史上最全的Redis基础+进阶项目实战总结笔记
Unity realizes rotation and Revolution
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Is the Flink connector JDBC open source? Where can I download it
Paging query, using jdbc-- paging query
0 basic unity course. Bricklaying
Wildcard SSL certificate issuing time
深度学习------不同方法实现Inception-10
Deep learning ----- different methods to realize inception-10
IO stream, character read / write, copy
Transport layer protocol tcp/udp
Network high concurrency