二分法,挺难的。。。。。。我好累
class Solution(object):
def search(self, nums, target):
:type nums: List[int]
:type target: int
:rtype: int
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if target >= nums[0]:
# 0-mid 均为上升区间
if nums[mid] >= nums[0] and target > nums[mid]:
left = mid + 1
else:
right = mid - 1
else:
if nums[mid] >= nums[0] or target > nums[mid]:
left = mid + 1
else:
right = mid - 1
return -1
凑字数。。。
网友评论