码迷,mamicode.com
首页 > 编程语言 > 详细

153. Find Minimum in Rotated Sorted Array java solutions

时间:2016-06-25 17:48:20      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

Suppose a sorted array 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.

 

Subscribe to see which companies asked this question

 

 1 public class Solution {
 2     public int findMin(int[] nums) {
 3         if(nums == null) return 0;
 4         int low = 0,high = nums.length-1;
 5         if(nums[low] <= nums[high]) return nums[0];
 6         int mid = 0;
 7         while(low < high){
 8             mid = (low + high)/2;
 9             if(nums[mid] > nums[mid+1]) return nums[mid+1];// 后面的数字比前面小,就可以找到了。
10             if(nums[mid] > nums[low]) low = mid;
11             else high = mid;
12         }
13         return nums[0];
14     }
15 }

这里用二分做的时候,经常分不清楚情况。

153. Find Minimum in Rotated Sorted Array java solutions

标签:

原文地址:http://www.cnblogs.com/guoguolan/p/5616596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!