153. Find Minimum in Rotated Sorted Array
Posted warmland
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了153. Find Minimum in Rotated Sorted Array相关的知识,希望对你有一定的参考价值。
binary search但是三个数的时候单独处理
1 public int findMin(int[] nums) { 2 if(nums.length == 0) { 3 return 0; 4 } 5 int left = 0; 6 int right = nums.length - 1; 7 while(left < right - 2) { 8 if(nums[left] < nums[right]) { 9 return nums[left]; 10 } 11 int mid = left + (right - left) / 2; 12 if (nums[mid-1] < nums[left]) { 13 right = mid - 1; 14 } else if (nums[mid+1] > nums[right]){ 15 left = mid + 1; 16 } else { 17 left = mid - 1; 18 right = mid + 1; 19 } 20 } 21 return Math.min(Math.min(nums[left + (right - left)/2], nums[left]), nums[right]); 22 }
以上是关于153. Find Minimum in Rotated Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章
153. Find Minimum in Rotated Sorted Array
#Leetcode# 153. Find Minimum in Rotated Sorted Array
153. Find Minimum in Rotated Sorted Array
153. Find Minimum in Rotated Sorted Array