[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., [0,1,2,4,5,6,7]
might become [4,5,6,7,0,1,2]
).
You are given a target value to search. If found in the array return its index, otherwise return -1
.
You may assume no duplicate exists in the array.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2]
, target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2]
, target = 3
Output: -1
1) 这个题目思路就是先画图, 分区间讨论,注意是左移还是右移,最好画图判断下。
2)可以利用[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search 的思路,通过lg n 找到转折点,然后分别在两段里面找。
T: O(lgn)
Code
1)
class Solution:
def search(self, nums, target):
if not nums: return -1
S, E, l, r = nums[0], nums[-1], 0, len(nums) - 1
while l + 1 < r:
mid = l + (r - l)//2
if target == nums[mid]: return mid
if nums[mid] >= S:
if S <= target <= nums[mid]:
r = mid
else:
l = mid
else:
if nums[mid] <= target <= E:
l = mid
else:
r = mid
if nums[l] == target: return l
if nums[r] == target: return r
return -1
[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search的更多相关文章
- [LeetCode] 153. Find Minimum 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 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 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][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...
- Convert Sorted List to Binary Search Tree&&Convert Sorted Array to Binary Search Tree——暴力解法
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 154. Find Minimum in Rotated Sorted Array II(Binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...
- [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
随机推荐
- C#学习笔记(34)——委托传值(回忆版)
说明(2018-4-6 20:31:03): 1. 昨天晚上看三层,看完第一天的最后一节,会员的修改和增加,感觉欲仙欲死,果断关机睡觉. 2. 上午搞了半天哈利波特的原版书epub的下载,结果都没发现 ...
- spring boot与kafka
1.项目搭建 2.关键代码与配置 3.性能调优 注意,本项目基于spring boot 1,如果是spring boot 2有可能会报错.相应的包需要更新 1.项目搭建 kafka版本:kafka_2 ...
- mysql事务隔离级别及传播机制
TRANSACTION(事务隔离级别) 在说明事务隔离级别之前先说一下脏读.不可重复读.幻读这三个概念. 脏读:一个事务读取到另一事务未提交的更新新据.当一个事务正在访问数据,并且对数据进行了修改,而 ...
- Kafka Eagle Consumers不显示
原因: kafka.eagle.offset.storage配置有误 该配置的作用:# kafka offset storage -- Offset stored in a Kafka cluster ...
- shell md5sum
md5sum out.a echo "fd_limit=$(ulimit -n), fd_used=$(ll /proc/4741/fd | wc -l)" 需找句柄,及fd ...
- page load时执行JavaScript
// Multiple onload function created by: Simon Willison // http://simonwillison.net/2004/May/26/addLo ...
- PowerDesigner 15进行逆向工程生成数据库图表时,注释的comment的生成,解决PowerDesigner逆向工程没有列注释
使用PowerDesigner默认配置逆向工程是没有注释(name列为英文,comment列是空的),这样的不方便查看字段具体是什么意义,将注释一同导出,方便查看字段具体的意义,如下图 注释列导出步骤 ...
- git log --pretty=format:" "
控制显示的记录格式,常用的格式占位符写法及其代表的意义如下: 选项 说明%H 提交对象(commit)的完整哈希字串%h 提交对象的简短哈希字串%T 树对象(tree)的完整哈希字串% ...
- maven 私服同步无法获取依赖的pom.xml的依赖
项目中引入了依赖: <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hado ...
- Java课程寒假之开发记账本软件(网页版)之五
一.实现基本功能之后 可以添加其他功能,比如说添加账户,删除账户,以及查询页面的分页.(我都没写,滑稽) 二.基本功能部分截图