目录
Leetcode-34
Leetcode-35
Leetcode-69
Leetcode-74
Leetcode-240
Leetcode-153
Leetcode-278
Leetcode-162
Leetcode-287
Leetcode-275
以上十道Leetcode题目都可使用二分的思想来解决
在解题的过程中都使用了二分模板
在解决的过程中关键是要从题目中找到将总体分为两个部分的性质,从而使用二分法解决问题。
class Solution{
public:
vector searchRange(vector& nums, int target){
if (nums.empty()){
return {-1, -1};
}
int l = 0, r = nums.size() - 1;
while(l < r){
int mid = l + r >> 1;
if(nums[mid] >=target) r = mid;
else l = mid + 1;
}
if(nums[r] != target) return {-1, -1};
int start = r;
l = 0, r = nums.size() - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(nums[mid] <= target) l = mid;
else r = mid - 1;
}
int end = r;
return {start, end};
}
};
// class Solution{
// public:
// std::vector searchRange(std::vector &nums, int target){
// std::vector result;
// int left = left_bound(nums, target);
// result.push_back(left);
// int right = right_bound(nums, target);
// result.push_back(right);
// return result;
// }
// private:
// int left_bound(std::vector &nums, int target){
// int begin = 0;
// int end = nums.size()-1;
// while(begin <= end){
// int mid = (begin+end)/2;
// if(target == nums[mid]){
// if(mid == 0 || nums[mid-1] < target){
// return mid;
// }
// end = mid - 1;
// }
// else if(target < nums[mid]){
// end = mid - 1;
// }
// else if(target > nums[mid]){
// begin = mid + 1;
// }
// }
// return -1;
// }
// int right_bound(std::vector &nums, int target){
// int begin = 0;
// int end = nums.size() - 1;
// while(begin <= end){
// int mid = (begin + end) / 2;
// if(target == nums[mid]){
// if(mid == nums.size() - 1 || nums[mid + 1] >target){
// return mid;
// }
// begin = mid + 1;
// }
// else if(target < nums[mid]){
// end = mid - 1;
// }
// else if(target > nums[mid]){
// begin = mid + 1;
// }
// }
// return -1;
// }
// };
class Solution{
public:
int searchInsert(vector& nums, int target){
if (nums.empty() || nums.back() < target){
return nums.size();
}
int l = 0, r = nums.size() - 1;
while(l < r){
int mid = l + r >> 1;
if (nums[mid] >= target){
r = mid;
}
else{
l = mid + 1;
}
}
return r;
}
};
// class Solution{
// public:
// int searchInsert(std::vector &nums, int target){
// int index = -1;
// int begin = 0;
// int end = nums.size() - 1;
// while(index == -1){
// int mid = (begin+end)/2;
// if(target == nums[mid]){
// index = mid;
// }
// else if(target < nums[mid]){
// if(mid == 0 || target > nums[mid-1]){
// index = mid;
// }
// end = mid - 1;
// }
// else if(target > nums[mid]){
// if(mid == nums.size()-1 || target < nums[mid+1]){
// index = mid + 1;
// }
// begin = mid + 1;
// }
// }
// return index;
// }
// };
class Solution{
public:
int mySqrt(int x){
int l = 0, r = x;
while(l < r){
int mid = l + (long long)r + 1 >> 1;
if (mid <= x / mid) l = mid;
else r = mid - 1;
}
return r;
}
};
class Solution {
public:
bool searchMatrix(vector>& matrix, int target) {
if (matrix.empty() || matrix[0].empty()) return false;
int n = matrix.size(), m = matrix[0].size();
int l = 0, r = n * m - 1;
while(l < r){
int mid = l + r >> 1;
if(matrix[mid / m][mid % m] >= target) r = mid;
else l = mid + 1;
}
if(matrix[r / m][r % m] != target) return false;
return true;
}
};
class Solution:
def searchMatrix(self, matrix, target):
if not matrix:
return False
i = 0
j = len(matrix[0]) - 1
while i < len(matrix) and j >= 0:
value = matrix[i][j]
if value == target:
return True
elif value > target:
j -= 1
else:
i += 1
return False
class Solution {
public:
int findMin(vector& nums) {
int l = 0, r = nums.size() - 1;
while (l < r){
int mid = l + r >> 1;
if (nums[mid] <= nums.back()) r = mid;
else l = mid + 1;
}
return nums[r];
}
};
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l = 1, r = n;
while (l < r){
int mid = (long long)l + r >> 1;
if (isBadVersion(mid)) r = mid;
else l = mid + 1;
}
return r;
}
};
class Solution {
public:
int findPeakElement(vector& nums) {
int l = 0, r = nums.size() - 1;
while (l < r){
int mid = l + r >> 1;
if (nums[mid] > nums[mid + 1]) r = mid;
else l = mid + 1;
}
return r;
}
};
class Solution {
public:
int findDuplicate(vector& nums) {
int n = nums.size() - 1;
int l = 1, r = n;
while (l < r){
int mid = l + r >> 1;
int cnt = 0;
for (auto x : nums)
if (x >= l && x <= mid)
cnt++;
if (cnt > mid - l + 1) r = mid;
else l = mid + 1;
}
return r;
}
};
class Solution {
public:
int hIndex(vector& citations) {
int l = 0, r = citations.size();
while(l < r){
int mid = l + r + 1 >> 1;
if(citations[citations.size() - mid] >= mid) l = mid;
else r = mid - 1;
}
return r;
}
};