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. DirectX11 With Windows SDK--18 使用DirectXCollision库进行碰撞检测

    前言 在DirectX SDK中,碰撞检测的相关函数位于xnacollision.h中.但是现在,前面所实现的相关函数都已经转移到Windows SDK的DirectXCollision.h中,并且处 ...

  2. NOI-OJ 2.2 ID:8758 2的幂次方表示

    思路 可以把任意一个数转化为2^a+2^b+2^c+...+2^n 例如137的二进制为10001001,这就等效于2^7+2^3+2^0 以上结果如何通过程序循环处理呢?需要把数字n分解为上述公式, ...

  3. MongoDB 3.6.9 集群搭建 - 切片+副本集

    1. 环境准备 在Mongo的官网下载Linux版本安装包,然后解压到对应的目录下:由于资源有限,我们采用Replica Sets + Sharding方式来配置高可用.结构图如下所示: 这里我说明下 ...

  4. 解决Ubuntu 17.10设置面板打不开的问题

    问题描述 对于Ubuntu桌面系统我用得不多,最近安装了Ubuntu17.10使用,一直都没遇到什么大的问题,界面风格已经与Windows很相似,总体体验还不错.直到某一天我突然手痒痒把Dock面板从 ...

  5. [再寄小读者之数学篇](2014-09-22 distributions and square integrable functions)

    Suppose that $f\in L^2$, $g\in \scrD'$, if $$\bex f=g,\mbox{ in }\scrD', \eex$$ then $f=g\in L^2$. I ...

  6. MySQL的一些基本命令笔记(4)

    delete 语句嵌套子查询: delete from 表名1 where 列名 操作符 (select 列名 from 表名2 where 条件); 示例: delete from customer ...

  7. springboot集成elasticsearch遇到的问题

    public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String>{ Page<EsBl ...

  8. DQL、DML、DDL、DCL区别

    DQL(data query language)数据查询语言 主要是由SELECT构成的查询语句 基本语法:select 字段名 from 表名 where 查询条件 DML(data manipul ...

  9. uap

    1.UAP 从前端到后端 详细教程 (一) 博主友好几篇相关的文章

  10. vue之生命周期钩子函数之运用

    一.什么是生命周期钩子函数: 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等.同时在这个过程中也会运行 ...