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.
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left=0
right=len(nums)-1
while left <= right:
mid = (left+right)/2
if target == nums[mid]:
return mid
if nums[mid] >= nums[left]:
if target < nums[mid] and target >= nums[left]:
right=mid-1
else:
left=mid+1
elif nums[mid] < nums[right]:
if target > nums[mid] and target <= nums[right]:
left=mid+1
else:
right=mid-1
return -1
leetcode Search in Rotated Sorted Array python的更多相关文章
- 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 ...
随机推荐
- 蓝桥杯 BASIC 29 高精度加法(大数)
[思路]:大数处理都一样. [AC代码]:代码细节能够美化一下. #include <iostream> #include <algorithm> #include <c ...
- SSCTF-Final-Re-Play
SSCTF-Final-Re-Play 比赛时花了一晚上搞定了,结果写脚本的时候发送的内容忘记base64加密然后异或8了,手动测试的时候当然是这样做了,写脚本的时候脑抽了.这个题只有forx做出来 ...
- UVA 10570 Meeting with Aliens
题意: N个外星人围成一桌坐下,有序的排列指N在N-1与N+1中间,现在给出一个序列,问至少交换几次可以得到有序的序列. 分析: 复制一遍输入序列,放在原序列之后.相当于环.通过枚举,可以把最小交换次 ...
- How to recover a skipped tablespace after an incomplete recovery with resetlogs? [ID 1561645.1]
n this Document Goal Solution This document is being delivered to you via Oracle Support's Rapid ...
- PHP学习笔记-数组(1)
1-1 数组定义 1.什么是数组? 所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标.组 ...
- python image show()方法的预览问题
在windows下面使用PIL中Image的show()函数时,执行下列代码: from PIL import Image img = Image.open("1.png") ...
- Preorder, Inorder, and Postorder非递归总结
Preorder, Inorder, and Postorder Iteratively Summarization[1] 1.Pre Order Traverse public List<In ...
- 用python实现文件读取和内容替换
infile = open("D:/test.txt", "r") #打开文件 outfile = open("D:/pp2.txt", & ...
- MVC in Javascript
MVC in Javascript From http://www.cnblogs.com/tugenhua0707/p/5156179.html 原博的比我详细 我是以自己的惯用的方式实现了一下 M ...
- undo_retention:确定最优的撤销保留时间
使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...