Skip to content

Commit af6df47

Browse files
committed
feat: find-minimum-in-rotated-sorted-array-ii
1 parent 4dadb39 commit af6df47

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
154. 寻找旋转排序数组中的最小值 II
7+
https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/
8+
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
9+
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
10+
请找出其中最小的元素。
11+
注意数组中可能存在重复的元素。
12+
"""
13+
def findMin(self, nums: List[int]) -> int:
14+
l, r = 0, len(nums) - 1
15+
while l < r:
16+
mid = l + (r - l) // 2
17+
# 左侧是单调的,旋转发生在右侧
18+
if nums[r] < nums[mid]:
19+
l = mid + 1
20+
elif nums[r] > nums[mid]:
21+
r = mid
22+
else:
23+
r -= 1
24+
25+
return nums[l]
26+
27+
28+
def findMinByForce(self, nums: List[int]) -> int:
29+
for num in nums:
30+
if num < nums[0]:
31+
return num
32+
33+
return nums[0]
34+
35+
36+
so = Solution()
37+
# print(so.findMin([3,4,5,1,2]))
38+
print(so.findMin([3,3,1,3]))

0 commit comments

Comments
 (0)