Find Minimum in Rotated Sorted Array

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.

class Solution {
public:
    int findMin(vector<int>& nums) {
        int n=nums.size();
        int res,res1=nums[0],res2=nums[n-1],i,j=n;
        if(n==1)
        return nums[0];
       // if(nums[0]<nums[1])
       // return res1;
        for(i=0;i<n&&i<j;i++)
        {
            if(nums[i]>nums[i+1])
            res1=nums[i+1];
            if(nums[j-1]<nums[j-2])
            res2=nums[j-1];
          //  return res2;
            res=min(res1,res2);
            j--;
        }
        return res;
    }
};
posted @ 2015-11-28 15:30  djiankuo  阅读(145)  评论(0编辑  收藏  举报