classSolution { public: intfindMin(vector<int>& nums){ int len = nums.size(); int l = 0, r = len - 1; while (l <= r) { int mid = (l + r) / 2; if (nums[l] <= nums[r]) { return nums[l]; } if (nums[mid] <= nums[r]) { r = mid; // if mid == r, then l == r, so nums[l] == num[r] } else { l = mid + 1; // l won't be bigger than r } } return nums[r]; } };
classSolution { public: intfindMin(vector<int>& nums){ int len = nums.size(); int l = 0, r = len - 1; while (l <= r) { int mid = (l + r) / 2; if (nums[l] <= nums[r]) { return nums[l]; } if (nums[mid] > nums[mid + 1]) { // 在l和r不相等时,mid+1的下标是在范围内的 return nums[mid + 1]; } if (nums[mid] > nums[l]) { l = mid + 1; // l won't be bigger than r } else { r = mid; } } return nums[r]; } };