hot100 review

56. 合并区间

https://leetcode.cn/problems/merge-intervals/description/?envType=study-plan-v2&envId=top-100-liked

该怎么排序区间
vector<vector>& intervals
sort(intervals)即可

238. 除自身以外数组的乘积

https://leetcode.cn/problems/product-of-array-except-self/description/?envType=study-plan-v2&envId=top-100-liked

41. 缺失的第一个正数

https://leetcode.cn/problems/first-missing-positive/?envType=study-plan-v2&envId=top-100-liked

240. 搜索二维矩阵 II

选择右上角的数字,比target大,往左,比target小,往下;
alt text

148. 排序链表

https://leetcode.cn/problems/sort-list/?envType=study-plan-v2&envId=top-100-liked

146. LRU 缓存

https://leetcode.cn/problems/lru-cache/description/?envType=study-plan-v2&envId=top-100-liked

215. 数组中的第K个最大元素

先写一个比较器,小根堆

class myCompar {
    // 小根堆
public:
    bool operator()(int left, int right) {
        return left > right;
    }
};

堆大小控制在k,最终在堆中的k个数都是最大的k个数字。

  1. 遍历数组,堆size < k, 直接push。
  2. 否则看元素大小,比堆顶大的话,pop堆顶,push元素
for (int num : nums) {
    if (pri_que.size() < k) {
        pri_que.push(num);
    } else {
        if (num > pri_que.top()) {
            pri_que.pop();
            pri_que.push(num);
        }
    }
}
posted @ 2024-10-13 23:55  Leome  阅读(22)  评论(0)    收藏  举报