Posted onEdited onInLeetCodeDisqus: Symbols count in article: 343Reading time ≈1 mins.
二分O(logn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intfindMin(vector<int>& nums){ int n = nums.size(); int l = 0, r = n - 1; while (l < r) { int m = l + (r - l) / 2; if (nums[m] > nums[r]) { // compare m with r rather than l because m could be l but never be r (in that case, m == r == l, impossible) l = m + 1; } else { r = m; } } return nums[l]; } };