leetcode 【 Search in Rotated Sorted Array 】python 实现
题目:
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
代码:oj测试通过 Runtime: 53 ms
class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return an integer
def search(self, A, target):
# none case & zero case
if A is None or len(A)==0 :
return -1
# binary search
start = 0
end = len(A)-1
while start<=end :
# one element left case
if start == end :
if A[start]==target :
return start
else:
return -1
# two elements left case
if start+1 == end :
if A[start]==target :
return start
elif A[end]==target :
return end
else:
return -1
# equal or more than three elements case
mid = (start+end)/2
if A[mid]==target :
return mid
elif A[mid]>target:
if A[start]>A[mid] and A[end]<A[mid]:
start = mid+1
elif A[start]<A[mid] and A[end]<A[mid]:
if A[end]>=target:
start = mid+1
else:
end = mid-1
elif A[start]>A[mid] and A[end]>A[mid]:
end = mid-1
else:
end = mid-1
else:
if A[start]>A[mid] and A[end]<A[mid]:
end = mid-1
elif A[start]<A[mid] and A[end]<A[mid]:
start = mid+1
elif A[start]>A[mid] and A[end]>A[mid]:
if A[end]>=target :
start = mid+1
else:
end = mid-1
else:
start = mid+1
return -1
思路:
这个就是binary search的思路。
个人没想出来什么好的方法,硬着头皮硬写了一个暴力解决方法。
传统的binary search只需要判断A[mid]与target的大小就可以了;但这道题是rotated array,光判断A[mid]是不够的。
还需要判断A[start] A[end]与A[mid]的大小才能判断,target可能落在[start,mid]区间还是[mid,end]区间。
自己的代码实在有些繁琐丑陋,估计有些if else条件可以合并,后续会改进。
leetcode 【 Search in Rotated Sorted Array 】python 实现的更多相关文章
- leetcode Search in Rotated Sorted Array python
#Suppose a sorted array is rotated at some pivot unknown to you beforehand. #(i.e., 0 1 2 4 5 6 7 ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- [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 7 migh ...
- 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 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
随机推荐
- <Android 基础(一)> Service
介绍 Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件.其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行.另外,一个组件能够 ...
- shell中的判断语句
1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空,变量) -z str ...
- pta 编程题14 Huffman Codes
其它pta数据结构编程题请参见:pta 题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路 ...
- java Vamei快速教程11 对象引用
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们之前一直在使用“对象”这个概念,但没有探讨对象在内存中的具体存储方式.这方面的 ...
- 知乎日报客户端应用ios源码
swift开发的知乎日报客户端详细源码,里面分UI和网络两个模块. 1.涉及到了大部分的UI控件的使用(甚至包括UIRefreshView,UITableConrol等等)2.Connection完成 ...
- Mybatis-连接池与事务
Mybatis 的连接池技术 Mybatis 将它自己的数据源分为三类: UNPOOLED 不使用连接池的数据源 POOLED 使用连接池的数据源 JNDI 使用 JNDI 实现的数据源 Mybati ...
- python实现二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- Bootstrap 提示工具(Tooltip)插件的事件
事件 下表列出了提示工具(Tooltip)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.tooltip 当调用 show 实例方法时立即触发该事件. $('#m ...
- 3170: [Tjoi2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1804 Solved: 968[Submit][Status][Discuss] Descript ...