LeetCode 154 尋找旋轉排序陣列中的最小值 II
阿新 • • 發佈:2021-01-07
技術標籤:LeetCode-二分查詢資料結構leetcode演算法
- 題目描述
假設按照升序排序的陣列在預先未知的某個點上進行了旋轉。
( 例如,陣列 [0,1,2,4,5,6,7] 可能變為 [4,5,6,7,0,1,2] )。
請找出其中最小的元素。
注意陣列中可能存在重複的元素。
- 題解
二分查詢
- 程式碼
class Solution { public: int findMin(vector<int>& nums) { int n=nums.size(); int l=0,h=n-1; while (l<h){ int m=l+((h-l)>>1); if (nums[m]>nums[h]) l=m+1; else if (nums[m]<nums[h]) h=m; else h--; } return nums[l]; } };