Open Zheaoli opened 2 years ago
/*
* @lc app=leetcode id=153 lang=typescript
*
* [153] Find Minimum in Rotated Sorted Array
*/
// @lc code=start
function findMin(nums: number[]): number {
let min = nums[0];
let left = 0;
let right = nums.length - 1;
while (left <= right) {
// if left to right is sorted
// then the left is the minimum value
if (nums[left] < nums[right]) {
min = Math.min(nums[left], min);
break;
}
const middle = ~~((left + right) / 2);
min = Math.min(nums[middle], min);
// if the middle value is greater or equal to left value
// then we need to search right portion
if (nums[middle] >= nums[left]) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return min;
};
// @lc code=end
微信id: 弘树 来自 vscode 插件
2022-09-27