当前位置:网站首页>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;
}
};
边栏推荐
- Matrix calculation of Neural Network Introduction (pytoch)
- 20220606 Mathematics: fraction to decimal
- Model selection for neural network introduction (pytorch)
- Stroke prediction: Bayesian
- Configure opencv in QT Creator
- Leetcode skimming ---75
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
- Ind FHL first week
- QT creator uses OpenCV Pro add
猜你喜欢

Hands on deep learning pytorch version exercise solution - 2.3 linear algebra

LeetCode - 900. RLE iterator

I really want to be a girl. The first step of programming is to wear women's clothes

Secure in mysql8.0 under Windows_ file_ Priv is null solution

Rewrite Boston house price forecast task (using paddlepaddlepaddle)

ECMAScript--》 ES6语法规范 ## Day1

熵值法求权重

Hands on deep learning pytorch version exercise solution - 2.4 calculus

Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation

A complete answer sheet recognition system
随机推荐
What useful materials have I learned from when installing QT
Ut2014 learning notes
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Ind yff first week
Leetcode刷题---1385
Jetson TX2 刷机
Ut2014 supplementary learning notes
Introduction to deep learning linear algebra (pytorch)
Stroke prediction: Bayesian
Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
Synchronous vs asynchronous
神经网络入门之模型选择(PyTorch)
Leetcode刷题---832
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
七、MySQL之数据定义语言(二)
SQL Server Management Studio cannot be opened
Leetcode刷题---977
神经网络入门之矩阵计算(Pytorch)
Several problems encountered in installing MySQL under MAC system
Leetcode刷题---75