当前位置:网站首页>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;
}
};
边栏推荐
- 熵值法求权重
- 二分查找法
- Leetcode刷题---217
- Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
- Configure opencv in QT Creator
- 侯捷——STL源码剖析 笔记
- [LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
- Several problems encountered in installing MySQL under MAC system
- Leetcode刷题---44
- 20220601 Mathematics: zero after factorial
猜你喜欢

Preliminary knowledge of Neural Network Introduction (pytorch)

Advantageous distinctive domain adaptation reading notes (detailed)

The imitation of jd.com e-commerce project is coming

ThreadLocal原理及使用场景

Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing

Free online markdown to write a good resume

High imitation wechat

Leetcode刷题---367

深度学习入门之自动求导(Pytorch)

Jetson TX2 brush machine
随机推荐
熵值法求权重
Leetcode刷题---10
mysql5.7安装和配置教程(图文超详细版)
Codeup: word replacement
20220531 Mathematics: Happy numbers
Model evaluation and selection
Synchronous vs asynchronous
Leetcode - 706 design hash mapping (Design)*
Leetcode刷题---75
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Jetson TX2 刷机
Judging the connectivity of undirected graphs by the method of similar Union and set search
Preliminary knowledge of Neural Network Introduction (pytorch)
20220606 Mathematics: fraction to decimal
Free online markdown to write a good resume
Ind FHL first week
Weight decay (pytorch)
Ut2012 learning notes