1. 画图, 直观。
2. 讨论数组为空或者个数为零。
3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者。
public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { // write your code here if(num == null || num.length == 0) return -1; // int small = num[0]; // for(int i = 1; i < num.length; i++) { // //small = (small < num[i]) ? small : num[i]; // if(small > num[i]) small = num[i]; // else small = small; // } // return small; int left = 0; int right = num.length - 1; if(num[left] < num[right]) return num[left]; else{ while(left + 1 < right){ int mid = (left + right) / 2; if(num[left] < num[mid]){ left = mid; } if(num[left] > num[mid]){ right = mid; } } if(num[left] < num[right]) return num[left]; else return num[right]; } } }
LintCode Find Minimum In Rotated Sorted Array
原文:http://www.cnblogs.com/LittleAlex/p/5491842.html