LC.153.Find Minimum in Rotated Sorted Array

Posted davidnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LC.153.Find Minimum in Rotated Sorted Array相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.
注意,我们要找的最小值,只会在右端,所以如果已经在右端,则挪RIGHT
如果在左端,则挪LEFT 往右端的深沟走

time: o(logn) space: o(1)
 1 public int findMin(int[] nums) {
 2         if (nums ==null || nums.length ==0) return -1 ;
 3         int left = 0, right = nums.length -1;
 4         while(left + 1 < right){
 5             int mid = left + (right - left)/2 ;
 6             if (nums[mid] < nums[right]){
 7                 right = mid ;
 8             } else{
 9                 //first half: left = mid, move to right
10                 left = mid ;
11             }
12         }
13         return nums[left]<nums[right]? nums[left] : nums[right] ;
14     }

 

以上是关于LC.153.Find Minimum in Rotated Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Minimum Absolute Difference in BST

153. Find Minimum in Rotated Sorted Array

#Leetcode# 153. Find Minimum in Rotated Sorted Array

153. Find Minimum in Rotated Sorted Array

Leetcode[154]-Find Minimum in Rotated Sorted Array II

LC.154. Find Minimum in Rotated Sorted Array II