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 targetexists, 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].
 
Approach #1:
class Solution {
public:
int search(vector<int>& nums, int target) {
int len = nums.size();
int l = 0, r = len-1;
while (l <= r) {
int m = l + (r - l) / 2;
if (nums[m] == target) return m;
else if (nums[m] > target) r = m - 1;
else l = m + 1;
}
return -1;
}
};

Runtime: 52 ms, faster than 31.47% of C++ online submissions for Binary Search.

704. Binary Search的更多相关文章

  1. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  2. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  3. 【Leetcode_easy】704. Binary Search

    problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...

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

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

  5. [LeetCode&Python] Problem 704. Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  6. [LeetCode] 704. Binary Search

    Description Given a sorted (in ascending order) integer array nums of n elements and a target value, ...

  7. 【LeetCode】704. Binary Search 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性查找 二分查找 日期 题目地址:https:// ...

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

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  9. LeetCode编程训练 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

随机推荐

  1. iOS开发之计算两个日期的时间间隔

    //首先创建格式化对象  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDate ...

  2. IO模型:同步、异步、阻塞、非阻塞

    前言: 在Linux的网络编程中,同步IO(synchronous IO).异步IO(asynchronous IO).阻塞IO(blocking IO).非阻塞IO(non-blocking IO) ...

  3. CSS 的导入方式 (link or import ?)

    前言 最常看见的CSS的使用方式有三种 1. 在span, div 等标签上直接使用 style 属性定义CSS <span style="color:blue">Th ...

  4. VBscript 做的设置网卡名称

    Set WSHShell=WScript.CreateObject("WScript.Shell") Dim NetcardDescriptionDim NetcardName i ...

  5. 优化你的服务器Apache、MySQL、PHP

    硬件上的考虑其实起50%的作用,当然是越快越好.如果不知道哪个快,就换成越贵越好.可实际上不可能做到这些,因为银子有限,所以按照这个顺序考虑:内存越大越好->硬盘SCSI好于SATA->C ...

  6. window.open() 父子页面的传值问题

    if(window.opener){//判断是否有父窗口,即打开本页面的窗口       window.opener.location.reload();//刷新父窗口       window.op ...

  7. Ubuntu 16.04 LTS 配置 Jupyter notebook 为服务器

    原材料: Ubuntu 16.04 LTS 64bit 已经配置好 IPython 和 Jupyter (安装步骤可以参照:http://www.cnblogs.com/McKean/p/619497 ...

  8. XML Schema笔记

    XML Schema是为了弥补DTD的不足而开发的一种新的用于约束和规范XML文档的标准 XML Schema作用: 定义可出现在文档中的元素定义可出现在文档中的属性定义哪些元素是子元素定义子元素的次 ...

  9. 判断IPv6地址合法性

    在 <netinet/in.h> 头文件下有下列这些宏用于判断IPv6地址合法性 返回0代表true,返回非零值代表ipv6地址为非指定类型的的地址(false) int IN6_IS_A ...

  10. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...