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 in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Note:

  1. You may assume that all elements in nums are unique.
  2. n will be in the range [1, 10000].
  3. The value of each element in nums will 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 二分搜索法的更多相关文章

  1. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  2. [01]Binary Search二分查找

    Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...

  3. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  4. LeetCode Binary Search Summary 二分搜索法小结

    二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...

  5. LeetCode 704. Binary Search (二分查找)

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  6. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  7. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  9. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

随机推荐

  1. [物理学与PDEs]第1章习题3 常场强下电势的定解问题

    在一场强为 ${\bf E}_0$ (${\bf E}_0$ 为常向量) 的电场中, 置入一个半径为 $R$ 的导电球体, 试导出球外电势所满足的方程及相应的定解条件. 解答: 设导电球体为 $B_R ...

  2. vmware 14 密钥

    VMware 2017 v14.x 永久许可证激活密钥 FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA CV7T2-6WY5Q-48EWP-ZXY7X-QGUWD 来源链接: http:/ ...

  3. [转] 常用Loss函数

    好文mark 转自机器之心 :https://www.jiqizhixin.com/articles/2018-06-21-3 “损失函数”是机器学习优化中至关重要的一部分.L1.L2损失函数相信大多 ...

  4. 【转】使用keepalived设置虚拟IP示例

    准备 准备两台机器,IP地址信息如下: host1: 192.168.56.103 host2: 192.168.56.104 为了测试需要,分别在两台机器上安装apache并启动httpd服务,使下 ...

  5. Linux下安装python的gmpy2库及遇到无法定位软件包的解决办法

    gmpy2需要gmp.h &mpfr.h &mpc.h 安装命令: sudo apt-get install libmpfr-dev libmpc-dev  成功之后再输入安装命令: ...

  6. 【原创】大叔问题定位分享(7)Spark任务中Job进度卡住不动

    Spark2.1.1 最近运行spark任务时会发现任务经常运行很久,具体job如下: Job Id  ▾ Description Submitted Duration Stages: Succeed ...

  7. 此主机支持Intel VT-x,但Intel VT-x处于禁用状态

    原因:未开启虚拟化技术 解决方法:https://www.cnblogs.com/jiefu/p/10711955.html

  8. 浅析Memcache和Redis

    想必开发的小伙伴们对Memcache和Redis都不陌生吧,最近正好在整理它们,于是就写一下博客吧!一方面是分享,另一方面便于自己查找. 首先,来说说Memcache和Redis是什么? 说得简单一点 ...

  9. CentOS7.X首次安装docker无法启动的问题解决

    CentOS7.2 随着Docker的不断流行与发展,docker公司(或称为组织)也开启了商业化之路,Docker 从 17.03版本之后分为 CE(Community Edition) 和 EE( ...

  10. docker修改默认存储位置

    前言:我这是默认安装的docker,所以其存放位置是/var/lib/docker,你可以发现是在/下面,之后你用docker 拉取的镜像文件也存放在这下面,这样就很快导致/空间爆满 1.修改配置文件 ...