LintCode Q159 Find Minimum in Rotated Sorted Array optimised code in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2016年12月29日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# @param nums: a rotated sorted array
# @return: the minimum number in the array
def findMin(self, nums):
# find break point in the input array
# time complexity: O(log(n))
# invalide input
if len(nums) == 0:
return
lo, hi = 0, len(nums) - 1
# sorted array
target = nums[-1]
res = -1
while lo <= hi:
mid = lo + (hi - lo)/2
if nums[mid] <= target:
hi = mid - 1
res = mid
else:
lo = mid + 1
return nums[res]
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。