Tuesday, January 12, 2016

[leetcode]Find Minimum in Rotated Sorted Array II

这种题off by one error太容易出了.. 到目前为止除了仔细检查和写好run code找error和记code之外没找到好办法
public class Solution {
    public int findMin(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right){
         int mid = (left+right)/2;
            if (nums[right] > nums[mid]){
          right = mid;
         }else if (nums[right] < nums[mid]){
          left = mid+1;
         }else{
          right--;
         }
        }
        return nums[left];
    }
}