当前位置:网站首页>二分查找法
二分查找法
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;
}
};
边栏推荐
- 『快速入门electron』之实现窗口拖拽
- Advantageous distinctive domain adaptation reading notes (detailed)
- Replace the files under the folder with sed
- 2.2 DP: Value Iteration & Gambler‘s Problem
- Free online markdown to write a good resume
- 2018 y7000 upgrade hard disk + migrate and upgrade black apple
- Cases of OpenCV image enhancement
- Realize an online examination system from zero
- LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
- GAOFAN Weibo app
猜你喜欢
![[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression](/img/ce/8c2ede768c45ae6a3ceeab05e68e54.jpg)
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression

3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation

4.1 Temporal Differential of one step

CV learning notes alexnet

Dictionary tree prefix tree trie

LeetCode - 919. Full binary tree inserter (array)

CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)

Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:

Leetcode - the k-th element in 703 data flow (design priority queue)

Timo background management system
随机推荐
20220603 Mathematics: pow (x, n)
. DLL and Differences between lib files
Leetcode - 5 longest palindrome substring
Standard library header file
Opencv histogram equalization
Yolov5 creates and trains its own data set to realize mask wearing detection
Notes - regular expressions
Simple real-time gesture recognition based on OpenCV (including code)
Realize an online examination system from zero
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Configure opencv in QT Creator
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
LeetCode - 900. RLE 迭代器
CV learning notes - edge extraction
20220603数学:Pow(x,n)
Judging the connectivity of undirected graphs by the method of similar Union and set search
Basic use and actual combat sharing of crash tool
Hands on deep learning pytorch version exercise solution - 2.6 probability
The underlying principle of vector
Data preprocessing - Data Mining 1