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

class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n=len(nums)
low=0
high=n-1
mid=(low+high)//2 while low!=mid:
if nums[mid]==target:
return mid
elif nums[mid]>target:
high=mid
mid=(high+low)//2
elif nums[mid]<target:
low=mid
mid=(high+low)//2
if nums[mid]==target:
return mid
elif nums[high]==target:
return high
else:
return -1

  

[LeetCode&Python] Problem 704. Binary Search的更多相关文章

  1. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. [LeetCode&Python] Problem 401. Binary Watch

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  4. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  5. [LeetCode&Python] Problem 693. Binary Number with Alternating Bits

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  6. [LeetCode&Python] Problem 868. Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  7. 【Leetcode_easy】704. Binary Search

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

  8. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

随机推荐

  1. Webpack学习-Loader

    什么是Loader? 继上两篇文章webpack工作原理介绍(上篇.下篇),我们了解到Loader:模块转换器,也就是将模块的内容按照需求装换成新内容,而且每个Loader的职责都是单一,只会完成一种 ...

  2. 深度学习硬件:CPU、GPU、FPGA、ASIC

    人工智能包括三个要素:算法,计算和数据.人工智能算法目前最主流的是深度学习.计算所对应的硬件平台有:CPU.GPU.FPGA.ASIC.由于移动互联网的到来,用户每天产生大量的数据被入口应用收集:搜索 ...

  3. freemarker导出word档

    1.word另存为xml:2.xml文件后缀名改成ftl:3.编写完整json字符串备用:4.修改ftl中动态字段为json中对应字段名:5.编写java代码自动生成word文件:(注意:换行用< ...

  4. idea的破解及相关安装

    ---- idea的破解 -javaagent:../bin/JetbrainsCrack-2.7-release-str.jar 复制到相关的idea配置文件 并将该Jar包复制到idea的bin目 ...

  5. 修改create-react-app支持多入口

    使用Facebook官方脚手架create-react-app创建React应用,默认只能生成一个SPA,入口是index.html.虽然,SPA的页面切换可以使用前台路由框架方便(比如React-R ...

  6. C语言常见易错题集(分析及解答)(仅可用于交流,勿用于商业用途)

    1.能正确表示a和b同时为正或同时为负的逻辑表达式是( D  ). A.(a>=0||b>=0)&&(a<0||b<0)             B.(a> ...

  7. vs2017 无法提交到tfs的 git存储库

    tfs 是2018版本 使用git 工具是可以提交成功. 使用vs2017的 就会一直提示 授权失败 也可以使用新安装的git https://blog.csdn.net/Meteor_s/artic ...

  8. python dns

  9. 怎么获取红米6 Pro的root权限

    红米6 Pro能有啥方法获得ROOT超级权限?做开发的人知道,android设备有ROOT超级权限,如果手机获得root相关权限,就能够实现更强大的功能,举例子,做开发的人部门的营销部门的妹子,使用一 ...

  10. ASP中替换掉换行符<br>

    function HtmlStrReplace(Str)  if Str="" or isnull(Str) then    HtmlStrReplace="" ...