(*leetcode_Array)Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array

  Total Accepted: 35966  Total Submissions: 109229 My Submissions
Question  Solution 

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.

Show Tags
Have you met this question in a real interview? 
Yes
 
No

Discuss

二分查找,不过要比较的是num[len-1]这个元素,找到比num[len-1]小的元素一直缩进即可

class Solution {
public:
    int findMin(vector &num) {
        
        int len = num.size();
        if(len==0)  return 0;
        int left = 0, right = len-1;
        while(leftnum[len-1])
                left = mid+1;
            else
                right = mid;     
        }
        return num[left];
    }
};


你可能感兴趣的:(刷题找工作)