# 中级算法 排序和搜索
# 颜色分类
# 前 K 个高频元素
# 数组中的第K个最大元素
function findKthLargest(nums, k) {
// 对数组进行降序排序
nums.sort((a, b) => b - a);
// 返回第 K 大元素
return nums[k - 1];
}
# 寻找峰值
# 在排序数组中查找元素的第一个和最后一个位置
function findFirstAndLast(arr, target) {
let firstIndex = -1;
let lastIndex = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
if (firstIndex === -1) {
firstIndex = i;
}
lastIndex = i;
}
}
return [firstIndex, lastIndex];
}
# 合并区间
# 搜索旋转排序数组
# 搜索二维矩阵
https://leetcode.cn/problems/search-a-2d-matrix/ (opens new window)
# 搜索二维矩阵 Ⅱ
https://leetcode.cn/problems/search-a-2d-matrix-ii/ (opens new window)