# 153. Find Minimum in Rotated Sorted Array

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/

> 内容描述

```
Suppose an array sorted in ascending order 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.

Example 1:

Input: [3,4,5,1,2] 
Output: 1
Example 2:

Input: [4,5,6,7,0,1,2]
Output: 0
```

## 解题方案

> 思路 1
******- 时间复杂度: O(NlgN)******- 空间复杂度: O(1)******

python大法好，一行sb AC, beats 100%，可能测试用例大多数都是基本有序的吧

```python
class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return nums[0]
```

> 思路 2
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******



一遍遍历看有没有降序的时候，有立马返回那个值，到最后都没有就返回nums[0]

30秒钟 Bug free，一遍AC, beats 100%

```python
class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 1:
            return nums[0]
        pivot = nums[0]
        for i in range(1, len(nums)):
            if nums[i] < pivot:
                return nums[i]
            pivot = nums[i]
        return nums[0]
```


> 思路 3
******- 时间复杂度: O(lgN)******- 空间复杂度: O(1)******


二分法，思路看代码一目了然，[leetcode第33题](https://github.com/apachecn/awesome-algorithm/blob/master/docs/Leetcode_Solutions/Python/033._search_in_rotated_sorted_array.md)这道题很类似，我画了图的，可以看看

beats 100%

```python
class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        l, r = 0, len(nums) - 1
        while l <= r:
            mid = l + ((r-l) >> 1)
            if nums[mid] < nums[mid-1]:
                return nums[mid]
            elif nums[mid] < nums[l]:
                r = mid - 1
            elif nums[mid] > nums[r]:
                l = mid + 1
            else:
                return nums[l]
```
