35. Search Insert Position(再做只需要想如何取区间)
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0, right = nums.size();
while(left < right){
int mid = left + (right-left)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] > target) right = mid;
else left = mid+1;
}
return right;
}
};
最重要的还是如何取区间
当right = nums.size(), 表示为左闭右开,此时终止循环时,left = right
74. Search a 2D Matrix(只需要想到思路)
class Solution {
private:
int get(vector<vector<int>> matrix, int index){
int m = matrix.size(), n = matrix[0].size();
//纵坐标
int i = index/n;
//横坐标
int j = index%n;
return matrix[i][j];
}
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[0].size();//m是竖列,n是横列
int left = 0, right = m*n-1;
while(left <= right){
int mid = left + (right-left)/2;
if(get(matrix, mid) > target) right = mid-1;
else if(get(matrix, mid) < target) left = mid+1;
else return true;
}
return false;
}
};
把二维的转换成一维的
162. Find Peak Element(*)
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size()-1;
while(left < right){
int mid = left + (right-left)/2;
if(nums[mid] > nums[mid+1]){
right = mid;
}else{
left = mid+1;
}
}
return right;
}
};
33. Search in Rotated Sorted Array(*)
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0, right = nums.size()-1;
while(left <= right){
int mid = left + (right-left)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] >= nums[left]){
//说明在断崖的左边,这里不要忘记等于
if(target >= nums[left] && target < nums[mid]){
right = mid-1;
}else{
left = mid+1;
}
}else{
if(target > nums[mid] && target <= nums[right]){
left = mid+1;
}else{
right = mid - 1;
}
}
}
return -1;
}
};
先确定断崖和mid的关系, 在找有序的部分
34. Find First and Last Position of Element in Sorted Array(*)
class Solution {
private:
int left_bound(vector<int>& nums, int target){
int left = 0, right = nums.size()-1;
while(left <= right){
int mid = left + (right-left)/2;
if(nums[mid] > target){
right = mid-1;
}else if(nums[mid] < target){
left = mid+1;
}else{
right = mid -1;
}
}
if(left >= nums.size()) return -1;
return nums[left] == target ? left: -1;
}
int right_bound(vector<int>& nums, int target){
int left = 0, right = nums.size()-1;
while(left <= right){
int mid = left + (right-left)/2;
if(nums[mid] > target){
right = mid-1;
}else if(nums[mid] < target){
left = mid+1;
}else{
left = mid +1;
}
}
if(right < 0) return -1;
return nums[right] == target ? right: -1;
}
public:
vector<int> searchRange(vector<int>& nums, int target) {
int lIndex = left_bound(nums, target);
int rIndex = right_bound(nums, target);
return {lIndex, rIndex};
}
};
153. Find Minimum in Rotated Sorted Array
class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.size() == 1) return nums[0];
int left = 0, right = nums.size()-1;
if(nums[left] < nums[right]) return nums[left];
while(left <= right){
int mid = left+(right-left)/2;
if(nums[mid+1] < nums[mid]) return nums[mid+1];
if(nums[mid-1] > nums[mid]) return nums[mid];
if(nums[mid] > nums[0]){
left = mid+1;
}else{
right = mid-1;
}
}
return -1;
}
};
167. Two Sum II - Input Array Is Sorted(OK)
lass Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int left = 0, right = numbers.size()-1;
while(left < right){
int sum = numbers[left] + numbers[right];
if(sum > target) right--;
else if(sum < target) left++;
else return {left+1, right+1};
}
return {-1, -1};
}
};
11. Container With Most Water(ok)
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0;
int left = 0, right = height.size()-1;
while(left < right){
int cur_area = min(height[left], height[right]) * (right-left);
res = max(res, cur_area);
if(height[left] < height[right]) left++;
else right--;
}
return res;
}
};