|
1 | | -###153. Find Minimum in Rotated Sorted Array |
| 1 | +# 153. Find Minimum in Rotated Sorted Array |
2 | 2 |
|
| 3 | +**<font color=red>难度: Medium</font>** |
3 | 4 |
|
4 | | -题目: |
5 | | -<https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/> |
| 5 | +## 刷题内容 |
6 | 6 |
|
| 7 | +> 原题连接 |
7 | 8 |
|
8 | | -难度: |
| 9 | +* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ |
9 | 10 |
|
10 | | -Medium |
| 11 | +> 内容描述 |
11 | 12 |
|
| 13 | +``` |
| 14 | +Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. |
12 | 15 |
|
| 16 | +(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). |
13 | 17 |
|
14 | | -思路一: |
15 | | - |
16 | | -O(N) 就不说了 |
17 | | - |
18 | | -思路二: |
19 | | - |
20 | | -想的是分治,两段分别找出最小值,然后取最小值,但是依旧没有利用题目特性,并且也是O(N). |
21 | | - |
22 | | -> We can do it in O(logn) using Binary Search. If we take a closer look at above examples, we can easily figure out following pattern: The minimum element is the only element whose previous element is greater than it. If there is no such element, then there is no rotation and first element is the minimum element. |
23 | | -
|
24 | | - |
25 | | -上面提到了一个特性,就是minimum element唯一一个它之前的element比它大的,如果不存在这个element,那么就没有rotation. |
| 18 | +Find the minimum element. |
26 | 19 |
|
| 20 | +You may assume no duplicate exists in the array. |
27 | 21 |
|
| 22 | +Example 1: |
28 | 23 |
|
29 | | -思路其实是判断前半部分或者后半部分是否有序,然后来剔除,这里需要注意有比较多的边界case,因为如果就两个,那么会有特殊case 0 ,1 mid = 0,所以可以看一下,它这个处理,最后一个elif 是来比较mid 和 end |
| 24 | +Input: [3,4,5,1,2] |
| 25 | +Output: 1 |
| 26 | +Example 2: |
30 | 27 |
|
| 28 | +Input: [4,5,6,7,0,1,2] |
| 29 | +Output: 0 |
| 30 | +``` |
31 | 31 |
|
| 32 | +## 解题方案 |
32 | 33 |
|
| 34 | +> 思路 1 |
| 35 | +******- 时间复杂度: O(N)******- 空间复杂度: O(1)****** |
33 | 36 |
|
34 | 37 |
|
35 | | -``` |
36 | | -class Solution(object): |
37 | | - def findMin(self, nums): |
38 | | - """ |
39 | | - :type nums: List[int] |
40 | | - :rtype: int |
41 | | - """ |
42 | | - def findRotatedMin(nums, start, end): |
43 | | - if end < start: |
44 | | - return nums[0] |
45 | | - if start == end: |
46 | | - return nums[start] |
47 | | - mid = (start + end) / 2 |
48 | | - if mid > start and nums[mid] < nums[mid-1]: |
49 | | - return nums[mid] |
50 | | - elif mid < end and nums[mid+1] < nums[mid]: |
51 | | - return nums[mid+1] |
52 | | - elif nums[mid] < nums[end]: |
53 | | - return findRotatedMin(nums,start, mid -1) |
54 | | - return findRotatedMin(nums, mid+1, end) |
55 | | -
|
56 | | -
|
57 | | - return findRotatedMin(nums,0,len(nums)-1) |
58 | 38 |
|
59 | | -``` |
| 39 | +一遍遍历看有没有降序的时候,有立马返回那个值,到最后都没有就返回nums[0] |
60 | 40 |
|
61 | | -非递归 |
| 41 | +30秒钟 Bug free,一遍AC |
62 | 42 |
|
63 | | -``` |
| 43 | +```python |
64 | 44 | class Solution(object): |
65 | 45 | def findMin(self, nums): |
66 | 46 | """ |
67 | 47 | :type nums: List[int] |
68 | 48 | :rtype: int |
69 | 49 | """ |
70 | | - l,r = 0, len(nums) - 1 |
71 | | - while l < r: |
72 | | - mid = (l+r) / 2 |
73 | | - if mid > l and nums[mid] < nums[mid-1]: |
74 | | - return nums[mid] |
75 | | - elif mid < r and nums[mid] > nums[mid+1]: |
76 | | - return nums[mid+1] |
77 | | - elif nums[mid] < nums[r]: |
78 | | - r = mid -1 |
79 | | - else: |
80 | | - l = mid +1 |
| 50 | + if len(nums) == 1: |
| 51 | + return nums[0] |
| 52 | + pivot = nums[0] |
| 53 | + for i in range(1, len(nums)): |
| 54 | + if nums[i] < pivot: |
| 55 | + return nums[i] |
| 56 | + pivot = nums[i] |
81 | 57 | return nums[0] |
82 | | -
|
83 | 58 | ``` |
84 | | - |
85 | | - |
0 commit comments