一,搜索二维矩阵
240. 搜索二维矩阵 II - 力扣(LeetCode)
https://leetcode.cn/problems/search-a-2d-matrix-ii/?plan=data-structures&plan_progress=zz5yyb3
1,二分查找
思路与算法
由于矩阵matrix 中每一行的元素都是升序排列的,因此我们可以对每一行都使用一次二分查找,判断 target 是否在该行中,从而判断 target 是否出现。
class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { for (const auto& row: matrix) { auto it = lower_bound(row.begin(), row.end(), target); if (it != row.end() && *it == target) { return true; } } return false; } };
二,无重叠区间
435. 无重叠区间 - 力扣(LeetCode)
https://leetcode.cn/problems/non-overlapping-intervals/?plan=data-structures&plan_progress=zz5yyb3