[leetcode]Search in Rotated Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
题意:
Suppose a sorted array 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.
解题思路:二分查找的变种。
代码:
class Solution:# @param A, a list of integers# @param target, an integer to be searched# @return an integerdef search(self, A, target):left = 0; right = len(A) - 1while left <= right:mid = (left + right) / 2if target == A[mid]:return midif A[mid] >= A[left]:if target < A[mid] and target >= A[left]:right = mid - 1else:left = mid + 1elif A[mid] < A[right]:if target > A[mid] and target <= A[right]:left = mid + 1else:right = mid - 1return -1
[leetcode]Search in Rotated Sorted Array @ Python相关推荐
- LeetCode Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- leetcode - Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- leetcode:Search in Rotated Sorted Array
题目要求: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
- [leetcode]Search in Rotated Sorted Array II
这道题目仍然是二分,去掉不可能的部分.用了递归,在重复的情况下,就是有可能最左边的和最右边的相等,此时就不能直接判断出区间外的元素,左右两边同时递归.有重复元素的时候,在bad case的时候会退化为 ...
- [LeetCode] Search in Rotated Sorted Array
二分 : 判断条件 当a[left] <= a[mid]时,可以肯定a[left..mid]是升序的 循环有序 一共有以下两种情况 第一种 / / / / / / 条件: (A[mid] &g ...
- python旋转排序数组_LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现...
题目描述: python实现Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2, ...
- LeetCode 33. Search in Rotated Sorted Array
问题链接 LeetCode 33. Search in Rotated Sorted Array 题目解析 给定一个 "升序" 的 无重复 数组,从中寻找目标值."升序& ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
最新文章
- 盘点:2021年度物理学十大突破|《物理世界》
- 16.oauth2 + oidc 实现 client部分
- Avalonia跨平台入门第十二篇之动画效果
- 谷粒商城---新增收货地址,设置默认地址实现
- Unity新项目如何快速理清顶层代码结构
- Hystrix 熔断器舱壁模式
- NoSQL Manager for MongoDB 连接配置
- Bootstrap入门!
- jeesit 基于springMVC + mybatis配置多数据源的问题
- arm linux 看库的依赖,Linux命令查看X86平台 ARM平台上可执行文件与库的依赖关系...
- 【单片机】4.2 AT89S52中断系统结构
- php文件如何转换成mp4,qlv文件如何转换成mp4 怎样把下载好的qlv格式视频转换成MP4格式...
- wi7计算机桌面删除,如何删除win7系统桌面IE图标|win7删除桌面IE图标的方法
- Royal TSX安装使用
- 哥尼斯堡七桥问题用计算机,哥尼斯堡七桥问题解法真的解不出来?请尽快解答.急...
- 极客日报:雷军:小米高端手机正式对标苹果;腾讯要求抖音为《斗罗大陆》赔偿8亿;Ember 4.0发布
- Python2写csv文件中文乱码问题及解决方法详解
- scrapy爬取站长素材
- 爱康云前端项目结构和开发规范
- C#泛型List 的定义、作用、用法