Wednesday, October 22, 2014

Leetcode: Find Minimum in Rotated Sorted Array @Python

Pay attention to the ending condition and edge cases.
class Solution:
    # @param num, a list of integer
    # @return an integer
    def findMin(self, num):
        n=len(num)
        strt=0
        nd=n-1
        while num[strt]>num[nd]:
            mid=(strt+nd)//2
            if num[strt]>num[mid]:
                nd=mid
            elif num[strt]<num[mid]:
                strt=mid
            else:
                return num[nd]
        return num[strt]

No comments :

Post a Comment