当前位置:网站首页>二分查找法
二分查找法
2022-07-03 09:26:00 【UniversalNature】
来自 704. 二分查找
框架如下:
// 版本二
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size(); // 定义target在左闭右开的区间里,即:[left, right)
while (left < right) {
// 因为left == right的时候,在[left, right)是无效的空间,所以使用 <
int middle = left + ((right - left) >> 1);
if (nums[middle] > target) {
right = middle; // target 在左区间,在[left, middle)中
} else if (nums[middle] < target) {
left = middle + 1; // target 在右区间,在[middle + 1, right)中
} else {
// nums[middle] == target
return middle; // 数组中找到目标值,直接返回下标
}
}
// 未找到目标值
return -1;
}
};
边栏推荐
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Matplotlib drawing
- [LZY learning notes -dive into deep learning] math preparation 2.1-2.4
- 侯捷——STL源码剖析 笔记
- Out of the box high color background system
- [LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
- 20220610 other: Task Scheduler
- Opencv feature extraction sift
猜你喜欢
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
Implementation of "quick start electronic" window dragging
Deep learning by Pytorch
LeetCode - 919. Full binary tree inserter (array)
LeetCode - 5 最长回文子串
LeetCode - 715. Range module (TreeSet)*****
【C 题集】of Ⅵ
CV learning notes - image filter
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
随机推荐
Pytorch ADDA code learning notes
Label Semantic Aware Pre-training for Few-shot Text Classification
2.1 Dynamic programming and case study: Jack‘s car rental
Simple real-time gesture recognition based on OpenCV (including code)
Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
2.2 DP: Value Iteration & Gambler‘s Problem
Discrete-event system
20220608其他:逆波兰表达式求值
The underlying principle of vector
20220607其他:两整数之和
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
Leetcode - the k-th element in 703 data flow (design priority queue)
Label Semantic Aware Pre-training for Few-shot Text Classification
Data preprocessing - Data Mining 1
20220605数学:两数相除
Opencv gray histogram, histogram specification
LeetCode - 919. Full binary tree inserter (array)
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
20220606数学:分数到小数
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation