关闭

153. Find Minimum in Rotated Sorted Array **

71人阅读 评论(0) 收藏 举报
分类:

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.

You may assume no duplicate exists in the array.

https://discuss.leetcode.com/topic/4100/compact-and-clean-c-solution/9

I have thought of this method, but got confused about the ending condition.

class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        low =0
        high = len(nums)-1
        while low < high:
            if nums[low]<nums[high]:
                return nums[low]
            mid = (low+high)/2
            if nums[mid]>= nums[low]:
                low = mid +1
            else:
                high = mid
        return nums[low]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45839次
    • 积分:3740
    • 等级:
    • 排名:第8620名
    • 原创:331篇
    • 转载:87篇
    • 译文:0篇
    • 评论:9条
    最新评论