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. php编译安装后,加扩展模块

    1.进入php源码包中,找到需要安装的扩展模块目录. cd /root/php-5.6.26/ext/mbstring 2.在扩展模块目录,运行phpize程序. /usr/local/bin/php ...

  2. react jsx 数组变量的写法

    1.通过 map 方法 var students = ["张三然","李慧思","赵思然","孙力气","王萌 ...

  3. sort-list——链表、快慢指针找中间、归并排序

    Sort a linked list in O(n log n) time using constant space complexity. 链表,快慢指针找中点,归并排序. 注意判断条件fast-& ...

  4. NSArray中存的是实体时的排序

    NSArray中存储的是实体时的排序 by 伍雪颖 NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKe ...

  5. iOS开发核心语言Objective C —— 面向对象思维、setter和getter方法及点语法

    本分享是面向有意向从事iOS开发的伙伴们.或者已经从事了iOS的开发人员.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基础,建议您先翻阅我之前分享的iOS ...

  6. Android NDK JNI WARNING: illegal start byte 0x

    今天攻克了JNI WARNING: illegal start byte 0x81这个问题. 问题出现的现象是通过jni调用加密方法,调用之后返回密文内容,结果就出现这个问题. 在国外查找一段时间之后 ...

  7. java zip压缩优化版 解决压缩后文件一直被占用无法删除

    最近进行zip操作,从网上找到一个处理方法,但是经过试验存在一些bug,主要是文件流的申明存在问题,导致jvm一直占用文件而不释放,特意把自己修改的发出来,已备记录 import java.io.Bu ...

  8. hadoop shuffle

    1 hadoop shuffle的地位 hadoop  shuffle是map reduce算法的核心,是它连接了多个map和多个reduce,它将map的输出交给reduce作为输入. 2 hado ...

  9. yum lock

    状态 :睡眠中,进程ID:18439Another app is currently holding the yum lock; waiting for it to exit... 另一个应用程序是: ...

  10. 1250太小了 mysql 并发

    SHOW VARIABLES LIKE '%connection%'; character_set_connection utf8mb4collation_connection utf8mb4_gen ...