Skip to content

Commit d13281c

Browse files
authored
Update 153._find_minimum_in_rotated_sorted_array.md
1 parent 92fcb4b commit d13281c

File tree

1 file changed

+31
-58
lines changed

1 file changed

+31
-58
lines changed
Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,58 @@
1-
###153. Find Minimum in Rotated Sorted Array
1+
# 153. Find Minimum in Rotated Sorted Array
22

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

4-
题目:
5-
<https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/>
5+
## 刷题内容
66

7+
> 原题连接
78
8-
难度:
9+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
910

10-
Medium
11+
> 内容描述
1112
13+
```
14+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
1215
16+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
1317
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.
2619
20+
You may assume no duplicate exists in the array.
2721
22+
Example 1:
2823
29-
思路其实是判断前半部分或者后半部分是否有序,然后来剔除,这里需要注意有比较多的边界case,因为如果就两个,那么会有特殊case 0 ,1 mid = 0,所以可以看一下,它这个处理,最后一个elif 是来比较mid 和 end
24+
Input: [3,4,5,1,2]
25+
Output: 1
26+
Example 2:
3027
28+
Input: [4,5,6,7,0,1,2]
29+
Output: 0
30+
```
3131

32+
## 解题方案
3233

34+
> 思路 1
35+
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
3336

3437

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)
5838

59-
```
39+
一遍遍历看有没有降序的时候,有立马返回那个值,到最后都没有就返回nums[0]
6040

61-
非递归
41+
30秒钟 Bug free,一遍AC
6242

63-
```
43+
```python
6444
class Solution(object):
6545
def findMin(self, nums):
6646
"""
6747
:type nums: List[int]
6848
:rtype: int
6949
"""
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]
8157
return nums[0]
82-
8358
```
84-
85-

0 commit comments

Comments
 (0)