1 class Solution:
 2     def search(self, nums: 'List[int]', target: int) -> bool:
 3         n = len(nums)
 4         if n == 0:
 5             return False
 6         if n == 1:
 7             return target == nums[0]
 8         l,h = 0,n-1
 9         while l <= h:
10             mid = l + (h - l) // 2
11             if nums[l] == target or nums[mid] == target or nums[h] == target:
12                 return True
13             if nums[l] < nums[mid]:#左侧有序,以l为起点,以mid为终点
14                 if nums[l] < target and target < nums[mid]:
15                     h = mid - 1
16                 else:
17                     l = mid + 1
18             elif nums[l] > nums[mid]:#右侧有序,以mid为起点,以h为终点
19                 if nums[mid] < target and target < nums[h]:
20                     l = mid + 1
21                 else:
22                     h = mid - 1
23             else:#无法判断顺序,可以让h-1,也可以让l+1
24                 h -= 1
25         return False

 

posted on 2019-12-10 18:06  Sempron2800+  阅读(127)  评论(0编辑  收藏  举报