[LeetCode] 704. Binary Search_Easy tag: Binary Search
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 innumsand its index is 4
Example 2:
Input:nums= [-1,0,3,5,9,12],target= 2
Output: -1
Explanation: 2 does not exist innumsso return -1
Note:
- You may assume that all elements in
numsare unique. nwill be in the range[1, 10000].- The value of each element in
numswill 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的更多相关文章
- [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. ...
- [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 ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- 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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [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 ...
- [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 ...
- 将百分制转换为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 ...
- 【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 ...
随机推荐
- npm构建保存 code ELIFECYCLE解决办法
参考文档https://blog.csdn.net/gh254172840/article/details/78871573 使用npm构建报错 解决办法,进入工作目录 rm -rf node_mod ...
- 壁虎书4 Training Models
Linear Regression The Normal Equation Computational Complexity 线性回归模型与MSE. the normal equation: a cl ...
- [No0000B4].Net中String是引用类型还是值类型,答string是特殊的引用类型
using System; internal class Program { private static void Main() { //值类型 ; int b = a; a = ; Console ...
- CountDownLatch简单使用
CountDownLatch介绍 CountDownLatch是JAVA提供在java.util.concurrent包下的一个辅助类,可以把它看成是一个计数器,其内部维护着一个count计数,只不过 ...
- lame音频编码注意的点
1.注意每次编码的PCM数据不能太短,如果太短编码函数长期返回-1这样的错误,将导致编码器彻底失效程序奔溃的问题,经测试32000在3200下稳定,大概1/10码率是没有问题的,具体请自行查询或调试 ...
- arcengine实现右键菜单打开/关闭所有图层
参考资料: http://developer.51cto.com/art/201104/256774.htm 参照后自己做的: 关于右键菜单的几个有价值的网址: http://blog.csdn.n ...
- Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface
w 0-instanceof is used to determine whether a PHP variable is an instantiated object of a certain cl ...
- 20165225《Java程序设计》第三周学习总结
20165225<Java程序设计>第三周学习总结 1.视频与课本中的学习: 遇到的问题: 问题如下,无法编译. 最后经同学点出要放在同一个打包的文件夹里,于是就运行成功了,下面是4_15 ...
- centos 安装ss
yum install python-setuptools && easy_install pip pip install shadowsocks vi /etc/shadowsock ...
- bzoj 2563 [2012国家集训队Round 1 day2] 阿狸和桃子的游戏 贪心
正解:贪心 解题报告: 链接在这儿! 知道解法之后会jio的好像很简单的样子……其实挺难想到的QAQ 不过大佬讲了方法之后还是懂了 有一个很神仙的想法就是,你可以理解为每个点周围的边都有半个是自己的, ...