1. /**
    2. * leetcode #153 寻找旋转排序数组中的最小值
    3. *
    4. * 假设按照升序排序的数组在预先未知的某个点上进行了旋转
    5. *
    6. * 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]
    7. *
    8. * 请找出其中最小的元素。
    9. *
    10. * 示例 1
    11. * 输入:nums = [3,4,5,1,2]
    12. * 输出:1
    13. *
    14. * 示例 2
    15. * 输入:nums = [4,5,6,7,0,1,2]
    16. * 输出:0
    17. * **/
    18. function findMin(nums) {
    19. if (nums.length == 1) {
    20. return nums[0]
    21. }
    22. let l = 0, h = nums.length - 1;
    23. if (nums[h] > nums[l]) {
    24. return nums[0]
    25. }
    26. while (l <= h) {
    27. let m = Math.floor((l + h) / 2);
    28. if (nums[m] >= nums[0]) {
    29. l = m + 1;
    30. } else {
    31. h = m - 1;
    32. }
    33. }
    34. return nums[l]
    35. }
    36. console.log(findMin([2, 1]))