153. 寻找旋转排序数组中的最小值
class Solution {
public:
int findMin(vector<int>& nums) {
int l = 0, r = nums.size() - 1;
if(nums[0] > nums[r]){
while(l != r){
int mid = l + ((r - l) >> 1);
if(nums[mid] < nums[0]){
r = mid;
}else{
l = mid + 1;
}
}
}else{
while(l != r){
int mid = l + ((r - l) >> 1);
if(nums[mid] < nums[r]){
r = mid;
}else{
l = mid + 1;
}
}
}
return nums[l];
}
};
744. 寻找比目标字母大的最小字母
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
int l = 0, r = letters.size() - 1,mid;
if(letters[r]<=target) return letters[0];
while(l != r){
mid = l + ((r - l)>>1);
if(letters[mid] <= target){
l = mid + 1;
}else{
r = mid;
}
}
return letters[l];
}
};
Leetcode 35. 搜索插入位置
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()){
return 0;
}
int l = 0, r = nums.size() - 1, mid;
while(l < r){
mid = l + ((r - l)>>1);
if(nums[mid] == target){
return mid;
}else if(nums[mid] < target){
l = mid +1;
}else{
r = mid -1;
}
}
if(nums[l] < target)
return l + 1;
return l;
}
};