We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
153. 寻找旋转排序数组中的最小值
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
请找出其中最小的元素。
你可以假设数组中不存在重复元素。
示例 1:
输入: [3,4,5,1,2] 输出: 1 示例 2: 输入: [4,5,6,7,0,1,2] 输出: 0
The text was updated successfully, but these errors were encountered:
解题思路:
/** * @param {number[]} nums * @return {number} */ var findMin = function (nums) { if (nums.length < 2) { return nums[0]; } let left = 0; let right = nums.length - 1; if (nums[right] > nums[0]) { return nums[0]; } while (left < right) { let mid = Math.floor(left + (right - left) / 2); if (nums[mid] > nums[mid + 1]) { return nums[mid + 1]; } if (nums[mid - 1] > nums[mid]) { return nums[mid]; } if (nums[left] < nums[mid]) { left = mid + 1; } else { right = mid - 1; } } };
Sorry, something went wrong.
/** * @param {number[]} nums * @return {number} */ var findMin = function (nums) { const len = nums.length; if (len == 1) return nums[0]; let left = 0; let right = len - 1; // 左边小于右边,表示未旋转,直接将最左返回 if (nums[left] < nums[right]) return nums[left]; while (left < right) { const mid = Math.floor((left + right) / 2); if (nums[mid] > nums[mid + 1]) return nums[mid + 1]; if (nums[mid] < nums[mid - 1]) return nums[mid]; if (nums[left] < nums[mid]) { left = mid + 1; } else { right = mid - 1; } } };
No branches or pull requests
153. 寻找旋转排序数组中的最小值
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
请找出其中最小的元素。
你可以假设数组中不存在重复元素。
示例 1:
The text was updated successfully, but these errors were encountered: