###### tags: `LeetCode` `Binary Search` `Medium`
# LeetCode #153 [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
### (Medium)
已知一個長度為 n 的數組,預先按照升序排列,經由 1 到 n 次 旋轉 後,得到輸入數組。例如,原數組 nums = [0,1,2,4,5,6,7] 在變化後可能得到:
若旋轉 4 次,則可以得到 [4,5,6,7,0,1,2]
若旋轉 7 次,則可以得到 [0,1,2,4,5,6,7]
注意,數組 [a[0], a[1], a[2], ..., a[n-1]] 旋轉一次 的結果為數組 [a[n-1], a[0], a[1], a[2], ..., a[n-2]] 。
給你一個元素值 互不相同 的數組 nums ,它原來是一個升序排列的數組,並按上述情形進行了多次旋轉。請你找出並返回數組中的 最小元素 。
---
```
class Solution {
public:
int findMin(vector<int>& nums) {
int l=0, r=nums.size()-1;
while(l<r){
int m=(r-l)/2+l;
if(nums[m]>=nums[l]){
if(nums[m]>nums[r])l=m+1;
else r=m-1;
}
else{
if(nums[r]>nums[m])r=m;
else l=m;
}
}
return nums[l];
}
};
```