当前位置:网站首页>Binary search method
Binary search method
2022-07-03 10:35:00 【UniversalNature】
come from 704. Two points search
The framework is as follows :
// Version 2
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size(); // Definition target In the interval between left closed and right open , namely :[left, right)
while (left < right) {
// because left == right When , stay [left, right) It's invalid space , So use <
int middle = left + ((right - left) >> 1);
if (nums[middle] > target) {
right = middle; // target In the left range , stay [left, middle) in
} else if (nums[middle] < target) {
left = middle + 1; // target In the right range , stay [middle + 1, right) in
} else {
// nums[middle] == target
return middle; // Target value found in array , Return the subscript directly
}
}
// Target value not found
return -1;
}
};
边栏推荐
- Advantageous distinctive domain adaptation reading notes (detailed)
- Leetcode刷题---263
- Ut2016 learning notes
- Free online markdown to write a good resume
- mysql5.7安装和配置教程(图文超详细版)
- Leetcode刷题---202
- 7、 Data definition language of MySQL (2)
- What did I read in order to understand the to do list
- 20220606 Mathematics: fraction to decimal
- Leetcode刷题---278
猜你喜欢
Pytorch ADDA code learning notes
Install yolov3 (Anaconda)
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
Secure in mysql8.0 under Windows_ file_ Priv is null solution
LeetCode - 715. Range module (TreeSet)*****
Ut2016 learning notes
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
神经网络入门之矩阵计算(Pytorch)
六、MySQL之数据定义语言(一)
随机推荐
mysql5.7安装和配置教程(图文超详细版)
8、 Transaction control language of MySQL
Preliminary knowledge of Neural Network Introduction (pytorch)
Leetcode刷题---75
Leetcode-404: sum of left leaves
六、MySQL之数据定义语言(一)
20220605 Mathematics: divide two numbers
Leetcode刷题---263
A super cool background permission management system
Several problems encountered in installing MySQL under MAC system
High imitation wechat
权重衰退(PyTorch)
Configure opencv in QT Creator
Ut2013 learning notes
深度学习入门之线性代数(PyTorch)
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
Leetcode - the k-th element in 703 data flow (design priority queue)
A complete mall system
Leetcode刷题---10