当前位置:网站首页>leetcode704. Binary search (find an element, simple, different writing)
leetcode704. Binary search (find an element, simple, different writing)
2022-07-06 07:03:00 【Heavy garbage】


The main points of : Always maintain the same interval as the initial position .
1: Left open right open
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = -1, r = nums.size();
while (l + 1 < r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid;
else l = mid;
}
return -1;
}
};
2: Left and right closed
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid - 1;
else l = mid + 1;
}
return -1;
}
};
3: Left open right closed
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = -1, r = nums.size() - 1;
while (l < r) {
int mid = l + (r - l) / 2 + 1;//mid deviation r On one side , Otherwise, it will fall into a dead cycle
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid - 1;
else l = mid;
}
return -1;
}
};
4: Left closed right away
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size();
while (l < r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid;
else l = mid + 1;
}
return -1;
}
};
边栏推荐
- 配置树莓派接入网络
- [daily question] 729 My schedule I
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- 基于PyTorch和Fast RCNN快速实现目标识别
- 前缀和数组系列
- kubernetes集群搭建Zabbix监控平台
- Apache DolphinScheduler源码分析(超详细)
- Simple use of JWT
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- Establishment and operation of cloud platform open source project environment
猜你喜欢

基于PyTorch和Fast RCNN快速实现目标识别

Huawei equipment configuration ospf-bgp linkage

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

SAP SD发货流程中托盘的管理
![[brush questions] how can we correctly meet the interview?](/img/89/a5b874ba4db97fbb3d330af59c387a.png)
[brush questions] how can we correctly meet the interview?

树莓派3B更新vim

Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution

Windows Server 2016 standard installing Oracle

Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod

顶测分享:想转行,这些问题一定要考虑清楚!
随机推荐
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
开源的网易云音乐API项目都是怎么实现的?
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
SSO process analysis
顶测分享:想转行,这些问题一定要考虑清楚!
Call, apply, bind rewrite, easy to understand with comments
首发织梦百度推送插件全自动收录优化seo收录模块
UniPro甘特图“初体验”:关注细节背后的多场景探索
[daily question] 729 My schedule I
Apache dolphin scheduler source code analysis (super detailed)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
3. Business and load balancing of high architecture
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
攻防世界 MISC中reverseMe简述
Chapter 7 - thread pool of shared model
AI on the cloud makes earth science research easier
SAP SD发货流程中托盘的管理
Basic commands of MySQL
软件测试外包到底要不要去?三年真实外包感受告诉你
雲上有AI,讓地球科學研究更省力