159. Find Minimum in Rotated Sorted Array
Description
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).
Find the minimum element.
Example
Given [4, 5, 6, 7, 0, 1, 2] return 0
Notice
You may assume no duplicate exists in the array.
我的代码
class Solution:
"""
@param nums: a rotated sorted array
@return: the minimum number in the array
"""
def findMin(self, nums):
# write your code here
l = len(nums)
res = nums[0]
for i in range(1,l):
if nums[i-1]>nums[i]:
res = nums[i]
break
return res
- 时间复杂度: O(n)