Skip to content
New issue

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

154. Find Minimum in Rotated Sorted Array II #266

Open
Tcdian opened this issue Jul 22, 2020 · 1 comment
Open

154. Find Minimum in Rotated Sorted Array II #266

Tcdian opened this issue Jul 22, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jul 22, 2020

154. Find Minimum in Rotated Sorted Array II

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

请找出其中最小的元素。

注意数组中可能存在重复的元素。

Example 1

Input: [1,3,5]
Output: 1

Example 2

Input: [2,2,2,0,1]
Output: 0
@Tcdian
Copy link
Owner Author

Tcdian commented Jul 22, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
    let left = 0;
    let right = nums.length - 1;
    while(left < right) {
        const mid = (left + right) >> 1;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else if (nums[mid] < nums[right]) {
            right = mid;
        } else {
            right -= 1;
        }
    }
    return nums[left];
};
  • TypeScript Solution
function findMin(nums: number[]): number {
    let left = 0;
    let right = nums.length - 1;
    while(left < right) {
        const mid = (left + right) >> 1;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else if (nums[mid] < nums[right]) {
            right = mid;
        } else {
            right -= 1;
        }
    }
    return nums[left];
};

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant