您的位置:首页 > 其它

81 Search in Rotated Sorted Array II

2015-06-17 18:14 225 查看
class Solution:

# @param {integer[]} nums

# @param {integer} target

# @return {boolean}

def search(self, nums, target):

"considering the situation: nums[low]=nums[mid]=nums[upp] in which we would traverse the list, O(n)"

if not nums:

return False

l,u=0,len(nums)-1

while l<=u:

mid=(l+u)>>1 #for python, right shift means *2**(-n), and left shift refers to the operation *2**n

if target==nums[mid]:

return True

elif nums[l]==nums[mid]==nums[u]:

l,u=l+1,u-1

elif nums[l]<nums[mid] and nums[l]<=target<nums[mid] or nums[l]>nums[mid] and not(nums[mid]<target<=nums[u]):

u=mid-1

else: l=mid+1

return False
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: