Lintcode 159. Find Minimum in Rotated Sorted Array (Medium) (Python)

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.

Code:

class Solution:
    """
    @param nums: a rotated sorted array
    @return: the minimum number in the array
    """
    def findMin(self, nums):
        # write your code here
        left, right = 0, len(nums)-1
        mid = int((left+right)/2)
        while left < right:
            print('left:', left)
            print('mid:', mid)
            print('right:', right)
            print(' ')
            if nums[mid]<=nums[right]:
                right = mid
            else:
                left = mid + 1
            mid = int((left+right)/2)
        return nums[mid]

你可能感兴趣的:(Lintcode刷题,Lintcode,Python)