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].

因为是常规的没有duplicates的binary search, 所以参考[LeetCode] questions conclusion_ Binary Search1.1的思路和code即可.

Code

class Solution:
def search(self, nums, target):
l, r = 0, len(nums)-1
if target < nums[0] or target > nums[-1]: return -1
while l + 1 < r:
mid = l + (r-l)//2 # l + r maybe overflow, above 2**32
if nums[mid] > target:
r = mid
elif nums[mid] < target:
l = mid
else:
return mid if nums[l] == target:
return l
if nums[r] == target:
return r
return -1

[LeetCode] 704. Binary Search_Easy tag: Binary Search的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  3. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  4. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  9. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. D - Game Prediction

    Suppose there are M people, including you, playing a special card game. At the beginning, each playe ...

  2. ajax 获取服务器返回的XML字符串

    前台 解析失败不会抛出任何异常, 只会返回一个给定的错误文档 let l = console.log let http = ajanuw.create({ uri: 'http://localhost ...

  3. Thrift IDL

    Thrift类型 Thrift类型系统旨在允许程序员尽可能使用本机类型,无论使用何种编程语言.此信息基于并取代Thrift白皮书中的信息.Thrift IDL为每一种目标语言提供了用于生成代码的类型描 ...

  4. Flask web开发之路十二

    ge请求和post请求 ### get请求和post请求:1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放 ...

  5. span 英文数字保持一行,中文自动换行

    html 中 span 换行规则如下: span不换行默认只针对英文有效 如果想对中文设置有效需要添加样式 style="white-space:nowrap;" 默认的情况是这样 ...

  6. 使用 PREPARE 的几个注意点

    简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名.在其他的sql数据库中也是如此,mssql的解决方法是将整条sql语句作为变量,其中穿插变 ...

  7. Ubuntu设置DNS永久生效

    环境查看 设置临时生效,修改配置文件/etc/resolv.conf nameserver 202.96.134.133 重启失效 设置永久生效,修改网卡配置文件/etc/network/interf ...

  8. eclipse启动报错:Could not create the java virtual machine

    用maven.springboot开发时,安装了当时最新版的eclipse(3.5.5).eclipse解压版的非常方便,想先安装了看看.暂时没有升级其他软件. 打开的时候报错: 原因其实就是还木有升 ...

  9. MyBatis中choose when正确写法

    <choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <! ...

  10. C和C指针小记(六)-基本声明、指针声明、typedef 、常量、作用域、链接属性、存储类型、static

    1.变量的声明 声明变量的基本形式: 说明符号(一个或者多个) 声明表达式列表 说明符 (specifier) 包含一些关键字,用于描述被声明的标识符的基本类型,它也可用户改变标识符的缺省存储类型和作 ...