二分查找
- 题目链接
二分查找https://leetcode.cn/problems/binary-search/
- 算法原理
- 代码步骤
- 代码展示
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 + 1) / 2;
if(nums[mid] < target)
{
left = mid + 1;
}
else if(nums[mid] > target)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
};
在排序数组中查找元素的第一个和最后一个位置
- 题目链接
在排序数组中查找元素的第一个和最后一个位置https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/
- 算法原理
- 代码步骤
- 代码展示
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target)
{
if(nums.size() == 0)
{
return {-1, -1};
}
// 设置左端点和右端点
int begin = -1, end = -1;
int left = 0, right = nums.size() - 1;
while(left < right)
{
int mid = left + (right - left) / 2;
if(nums[mid] < target)
{
left = mid + 1;
}
else
{
right = mid;
}
}
if(nums[left] != target) return {-1, -1};
begin = left;
left = 0, right = nums.size() - 1;
while(left < right)
{
int mid = left + (right - left + 1) / 2;
if(nums[mid] <= target)
{
left = mid;
}
else
{
right = mid - 1;
}
}
if(nums[right] != target) return {-1, -1};
end = right;
return {begin, end};
}
};
x的平方根
- 题目链接
x的平方根https://leetcode.cn/problems/sqrtx/
- 算法原理
- 代码展示
class Solution {
public:
int mySqrt(int x)
{
if(x == 0) return 0;
int left = 1, right = x;
while(left < right)
{
long long mid = left + (right - left + 1) / 2;
if(mid * mid <= x)
{
left = mid;
}
else
{
right = mid - 1;
}
}
return left;
}
};
搜索插入位置
- 题目链接
搜索插入位置https://leetcode.cn/problems/search-insert-position/description/
- 算法原理
- 代码展示
class Solution {
public:
int searchInsert(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)
{
left = mid + 1;
}
else
{
right = mid;
}
}
if(nums[left] < target) return left + 1;
return left;
}
};
山脉数组的峰顶索引
- 题目链接
山脉数组的峰顶索引https://leetcode.cn/problems/peak-index-in-a-mountain-array/description/
- 算法原理
- 代码展示
class Solution {
public:
int peakIndexInMountainArray(vector<int>& arr)
{
// 最左侧和最右侧元素不可能使山顶
int left = 1, right = arr.size() - 2;
while(left < right)
{
int mid = left + (right - left + 1) / 2;
if(arr[mid] > arr[mid - 1])
{
left = mid;
}
else
{
right = mid - 1;
}
}
return left;
}
};
寻找峰值
- 题目链接
寻找峰值https://leetcode.cn/problems/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])
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left;
}
};
寻找旋转排序数组中的最小值
- 题目链接
寻找旋转排序数组中的最小值https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/description/
- 算法原理
- 代码展示
class Solution {
public:
int findMin(vector<int>& nums)
{
int left = 0, right = nums.size() - 1;
int n = nums.size();
while(left < right)
{
int mid = left + (right - left) / 2;
if(nums[mid] > nums[n - 1])
{
left = mid + 1;
}
else
{
right = mid;
}
}
return nums[left];
}
};
点名
- 题目链接
点名https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/description/
- 算法原理
- 代码展示
class Solution {
public:
int takeAttendance(vector<int>& records)
{
int left = 0, right = records.size() - 1;
while(left < right)
{
int mid = left + (right - left) / 2;
if(records[mid] == mid)
{
left = mid + 1;
}
else
{
right = mid;
}
}
// if(left == records.size() - 1 && records[left] == left) return left + 1;
// else return left;
return left == records[left] ? left + 1 : left;
}
};