赞
踩
1.LeetCode-69 求解Sqrt(x)
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; // mid * mid <= x;这样写法不好,会出现乘法溢出
else r = mid - 1;
}
return r;
}
};
2.LeetCode-35 搜索插入位置
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty() || nums.back() < target) return nums.size(); // nums.back() < target:插入的数字比当前数组中所有元素都大,直接返回数组的长度
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;
}
};
3.LeetCode-34 在排序数组中查找元素的第一个和最后一个位置
class Solution { public: vector<int> searchRange(vector<int>& 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}; // 二分出来的起始位置不等于target,则数组中不存在target这个数 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}; } };
4.LeetCode-74 搜索二维矩阵
class Solution { public: bool searchMatrix(vector<vector<int>>& 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; } };
5.LeetCode-153 寻找旋转排序数组中的最小值
class Solution {
public:
int findMin(vector<int>& nums) {
int l = 0, r = nums.size();
while(l < r){
int mid = l + r >> 1;
if(nums[mid] <= nums.back()) r = mid;
else l = mid + 1;
}
return nums[r];
}
};
6.LeetCode-33 搜索旋转排序数组
class Solution { public: int search(vector<int>& nums, int target) { if(nums.empty()) return -1; 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; } if(target <= nums.back()) r = nums.size() - 1; // target在后一段区间中 // 否则target在前一段区间中 else l = 0, r--; while(l < r){ int mid = l + r >> 1; if(nums[mid] >= target) r = mid; else l = mid + 1; } if(nums[l] == target) return l; return -1; } };
7.LeetCode-278 第一个错误的版本
// 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; // long long防止溢出
if(isBadVersion(mid)) r = mid;
else l = mid + 1;
}
return r;
}
};
8.LeetCode-162 寻找峰值
class Solution {
public:
int findPeakElement(vector<int>& 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;
}
};
9.LeetCode-287 寻找重复数
class Solution { public: int findDuplicate(vector<int>& nums) { // 抽屉原理 + 二分法可以解决 int l = 1, r = nums.size() - 1; while(l < r){ int mid = l + r >> 1; int cnt = 0; for(auto x: nums) if(x >= l && x <= mid) cnt++; // 苹果个数cnt if(cnt > mid - l + 1) r = mid; // 左半边区间中抽屉个数mid - l + 1 else l = mid + 1; } return r; } };
10.LeetCode-275 H指数 II
class Solution {
public:
int hIndex(vector<int>& nums) {
int l = 0, r = nums.size();
while(l < r){
int mid = l + r + 1 >> 1;
if(nums[nums.size() - mid] >= mid) l = mid;
else r = mid - 1;
}
return l;
}
};
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。