[LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
Example 1:
Input:nums= [-1,0,3,5,9,12],target= 9
Output: 4
Explanation: 9 exists innumsand its index is 4
Example 2:
Input:nums= [-1,0,3,5,9,12],target= 2
Output: -1
Explanation: 2 does not exist innumsso return -1
Note:
- You may assume that all elements in
numsare unique. nwill be in the range[1, 10000].- The value of each element in
numswill be in the range[-9999, 9999].
这道题就是最基本的二分搜索法了,这是博主之前总结的LeetCode Binary Search Summary 二分搜索法小结的四种之中的第一类,也是最简单的一类,写法什么很模版啊,注意right的初始化值,还有while的循环条件,以及right的更新值,这三点不同的人可能会有不同的写法,选一种自己最习惯的就好啦,参见代码如下:
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = , right = nums.size();
while (left < right) {
int mid = left + (right - left) / ;
if (nums[mid] == target) return mid;
else if (nums[mid] < target) left = mid + ;
else right = mid;
}
return -;
}
};
类似题目:
Search in a Sorted Array of Unknown Size
参考资料:
https://leetcode.com/problems/binary-search
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Binary Search 二分搜索法的更多相关文章
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- [01]Binary Search二分查找
Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- LeetCode Binary Search Summary 二分搜索法小结
二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...
- LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
随机推荐
- Web_0001:关于阿里云防盗链Referer,CDN加速,OSS自定义域名的操作
1,防盗链Referer 防止非法域名访问 OSS 数据,设置白名单: 允许为空 : 表示当从浏览器直接打开OSS资源时有效. 示例说明: 我在www.sojson.com里有一个www.baidu. ...
- MySQL关于日志配置安全整改及处理方法
[环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 需求:MySQL数据库都有每年的集团安全整改,常常要求弱口令扫描,基线扫描,漏洞扫描等等.对于MySQ ...
- webpack安装异常
webpack中文指南:https://zhaoda.net/webpack-handbook/index.html 今天中午,我用 cnpm install 重新下载了一下项目的依赖,爆了一 ...
- IIS短文件漏洞(搬运整理)
0x01. IIS短文件漏洞的由来 Microsoft IIS 短文件/文件夹名称信息泄漏最开始由Vulnerability Research Team(漏洞研究团队)的Soroush Dalili在 ...
- 设计模式十: 生成器模式(Builder Pattern)
简介 生成器模式属于创建型模式的一种, 又叫建造者模式. 生成器模式涉及4个关键角色:产品(Product),抽象生成器(builder),具体生成器(ConcreteBuilder),指挥者(Dir ...
- 重新定义Pytorch中的TensorDataset,可实现transforms
class TensorsDataset(torch.utils.data.Dataset): ''' A simple loading dataset - loads the tensor that ...
- HTTP协议12-HTTPS
1)HTTP的缺点 通信使用明文(不加密),内容可能会被窃听 不验证通信方的身份,因此有可能遭遇伪装 无法证明报文的完整性,所有有可能已被篡改 1.1 通信使用明文可能会被窃听 由于HTTP本身不具备 ...
- Linux shell 脚本总结
在中括号中,判断变量的值, 加不加双引号的问题? -z 判断 变量的值,是否为空: zero = 0 - 变量的值,为空,返回0,为true - 变量的值,非空,返回1,为false -n 判断变量的 ...
- Ubuntu18.04 LTS 安装部署golang最新版本1.10
1 步骤 //1 直接安装golang-go 目前最新版本是1.10 sudo apt-get install golang-go //2 向/etc/profile追加以下代码 sudo vim / ...
- iOS app内打开safari完成google的OAuth2认证
最近使用google的oauth认证,发现不再允许使用UIWebview进行认证了,必须使用系统游览器,使用游览器也不一定要在app之间跳转,ios使用SFSafariViewController就可 ...