数据结构刷题:第十八天(基础)

目录

一,搜索二维矩阵

1,二分查找

思路与算法

二,无重叠区间

 看题解:


一,搜索二维矩阵

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

 看题解:

猜你喜欢

转载自blog.csdn.net/m0_63309778/article/details/127060524
今日推荐