[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 ...
随机推荐
- Int32 最大的数值是多少???(附十进制十六进制相互转换且包含正负数的java代码)
正数转二进制很简单,转十六进制也很简单. 那么负数的情况下呢?在计算机中无法识别你给的符号“+”,"-",计算机只认识0和1 那么在二进制中如何表示负数. 先简单介绍一下负数如何转 ...
- 基于ASP.NET Core Data Protection生成验证token
ASP.NET Core Data Protection 不仅提供了非对称加密能力,而且提供了灵活的秘钥存储方式以及一致的加解密接口(Protect与Unprotect).Session中用到了它,C ...
- 牛客多校10 D Rikka with Prefix Sum 不是数据结构
https://www.nowcoder.com/acm/contest/148/D 题意: 1e5个数,1e5个操作,操作分为: 1.区间加. 2.整个数列替换为前缀和. 3.区间查询. 查询数小于 ...
- PHP之fopen wrappers模块
一.fopen wrappers模块的配置 ①.allow_url_fopen boolean //出于安全性考虑,此选项只能在 php.ini 中设置 //本选项激活了 URL 形式的 fopen ...
- HTML基础标签大全
HTML 标签大全及属性 常用的块级元素有 :div , from , table, p ,pre,h1-h5,dl,ol,ul 常用的内联元素:span ,a ,strong,em ,label , ...
- [skill] C语言数组名到底是个啥
1. 正常情况下,数组名是个地址常量. 2. sizeof(数组名)的时候,数组名就代表数字名,其类型为 type array[], 返回数组元素个数. 3. 除了2的情况以外,可以理解为一个指针常量 ...
- linux学习:【第1篇】初识Linux及安装
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第1篇]初识Linux及安装 写在前面 学习之初看了一段文章,很有感触,所以也 ...
- STS maven build 访问 jsp页面报错
STS 版本:spring-tool-suite-3.8.1.RELEASE-e4.6-win32-x86_64 maven版本:apache-maven-3.3.9 报错信息如图(图片解决方案来源博 ...
- Django 之Redis配置
目的:访问服务器频繁的读取数据库 ,会耗损服务器性能及降低用户体验,为此引入Redis 1,安装 redis(2.10.6兼容性更好) pip install redis 2,settings.py配 ...
- Jenkins tomcat打包启动脚本,待完善
shell脚本 #!/bin/bashJENKINS_HOME=/usr/local/jenkinsTOMCAT_HOME=/usr/local/jenkins/tomcat-testSHUTDOWN ...