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. HTML(二)HTML元素(整体结构,块级元素,内联元素,结构元素,交互元素,元素嵌套规则)

    HTML整体结构解释 <!DOCTYPE html> // 文件应以"<!DOCTYPE ......>"首行顶格开始,推荐使用"<!DOC ...

  2. [数学杂志]AML

    Copied from: http://www.elsevier.com/journals/applied-mathematics-letters/0893-9659/guide-for-author ...

  3. [物理学与PDEs]第1章第2节 预备知识 2.2 Ampere-Biot-Savart 定律, 静磁场的散度与旋度

    1. 电流密度, 电荷守恒定律 (1) 电荷的定向移动形成电流. (2) 电流密度 ${\bf j}$, 是描述导体内一点在某一时刻电流流动情况的物理量, 用单位时间内通过垂直于电流方向的单位面积的电 ...

  4. DUMP101 企业级电商FE

    需求拆分原则 1. 单个迭代不能太大 2. 需求可交付,功能闭环 3. 成本意识 二八法则 4.  预期价值体现 ……………………………………………………………………………… 做 [直接 git cl ...

  5. Java(6)for循环

    一.for循环的使用场合 1.1.while循环——先判断,再循环 while(1.条件表达式){      //2.循环操作      //3.更改循环条件表达式 } 1.2.do…while——先 ...

  6. ue4 材质表达式分类

    绿色节点 颜色 Color Desaturation 数学 Math GO 字体 Font FontSample,FontSampleParameter 实用程序 Utility 常用: Desatu ...

  7. 常见的cmake工程做法

    第一步,创建一个build目录存放cmake生成的中间文件: mkdir build 第二步,进入到build文件目录: cd build 第三步,cmake把代码文件生成一个makefile文件: ...

  8. gzip 压缩

    nginx 开启静态 gzip 配合 Vue 构建   在站点配置添加如下代码: location ~* \.(css|js)$ { gzip_static on; } 这是 nginx 的静态 gz ...

  9. LeetCode第十二题-将数字转化为罗马数字

    Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...

  10. VMware虚拟机从一台电脑复制到另一台电脑

    1.选中.vmx文件和所有的.vmdk文件,添加到压缩文件 vmx是虚拟系统配置文件,而vmdk则是虚拟磁盘文件,它们都是VMware所支持的文件格式 2.复制压缩文件到另一台电脑上,并解压 3.在另 ...