Skip to content

Commit f62cb36

Browse files
committed
solve problem Find Minimum In Rotated Sorted Array
1 parent c584719 commit f62cb36

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ All solutions will be accepted!
291291
|890|[Find And Replace Pattern](https://leetcode-cn.com/problems/find-and-replace-pattern/description/)|[java/py/js](./algorithms/FindAndReplacePattern)|Medium|
292292
|48|[Rotate Image](https://leetcode-cn.com/problems/rotate-image/description/)|[java/py/js](./algorithms/RotateImage)|Medium|
293293
|74|[Search A 2d Matrix](https://leetcode-cn.com/problems/search-a-2d-matrix/description/)|[java/py/js](./algorithms/SearchA2dMatrix)|Medium|
294+
|153|[Find Minimum In Rotated Sorted Array](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/)|[java/py/js](./algorithms/FindMinimumInRotatedSortedArray)|Medium|
294295

295296
# Database
296297
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Find Minimum In Rotated Sorted Array
2+
We can solve this problem by binary search
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int low = 0,
4+
high = nums.length - 1;
5+
6+
while (low < high - 1) {
7+
int mid = (low + high) / 2;
8+
9+
if (nums[mid] < nums[high])
10+
high = mid;
11+
else if (nums[mid] > nums[high])
12+
low = mid;
13+
}
14+
15+
return Math.min(nums[low], nums[high]);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function(nums) {
6+
let low = 0,
7+
high = nums.length - 1
8+
9+
while (low < high - 1) {
10+
let mid = parseInt((low + high) / 2)
11+
12+
if (nums[mid] < nums[high])
13+
high = mid
14+
else if (nums[mid] > nums[high])
15+
low = mid
16+
}
17+
18+
return Math.min(nums[low], nums[high])
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def findMin(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
low = 0
8+
high = len(nums) - 1
9+
10+
while low < high - 1:
11+
mid = (low + high) / 2
12+
13+
if nums[mid] < nums[high]:
14+
high = mid
15+
elif nums[mid] > nums[high]:
16+
low = mid
17+
18+
return min(nums[low], nums[high])

0 commit comments

Comments
 (0)