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. dreamware2018破解

    dreamware2018破解     C:\Program Files\Adobe\Adobe Dreamweaver CC 2018      

  2. Treap标准模板

    这是Treap的模板程序,支持Left/Right Rotate,Find the maxnum/minnum,Find the predecessor/successor of a node,Add ...

  3. 第7天:Q Quant库(未完待续)

    一.本文大纲: 1.Python内置函数计算期权的价格 2.numpy加速数值计算 3.SciPy进行仿真模拟 4.SciPy求解器计算隐含波动率 5.matplotlib绘图 二.案例 (看不懂,略 ...

  4. AngelToken揭秘区块链之四大链

    区块链,有着各种不同,与之相对应的就是内涵和功能.在区块链领域经常出现的四大链有:公有链.私有链.联盟链.许可链,这些链又分别可以为区块链干什么呢? 公有链(Public Blockchain) 是指 ...

  5. 【原创】KMP算法代码(C)

    //s是模式字符串,t是匹配字符串(可以看我上一篇文章中的叙述) int KMP(const char * s , const char * t) { int slen = strlen(s) , t ...

  6. git push后出错

    参考链接: 1,https://blog.csdn.net/shiren1118/article/details/7761203 2,http://www.cnblogs.com/xwdreamer/ ...

  7. websocket+rabbitMQ

    拉取镜像:docker run -d --hostname my-rabbit --name some-rabbit  -p 5672:5672 -p 15672:15672 -p 61613:616 ...

  8. [图形学]VS2017中OpenGL的下载及安装中的异常

    1.放dll文件:C:\Windows\SysWOW64 或C:\Windows\windows32 2.lib和h:C:\Program Files (x86)\Microsoft Visual S ...

  9. 手机端扫描证件识别SDK

    手机端扫描证件识别SDK 一.手机端扫描证件识别SDK应用背景 这些年,随着移动互联网的的发展,越来越多的公司都推出了自己的移动APP,这些APP多数都涉及到个人身份证信息的输入认证(即实名认证),如 ...

  10. Kubernetes 常用命令

    文章摘自:https://blog.csdn.net/felix_yujing/article/details/51622132 1 查看类命令--- # 查看集群信息 kubectl cluster ...