Data structure brushing questions: Day 18 (Basic)

 

Table of contents

First, search the two-dimensional matrix

1. Binary search

Ideas and Algorithms

Second, there is no overlapping interval

 Look at the solution:


First, search the two-dimensional matrix

240. Search Two-dimensional Matrix II - LeetCode https://leetcode.cn/problems/search-a-2d-matrix-ii/?plan=data-structures&plan_progress=zz5yyb3

1. Binary search

Ideas and Algorithms

Since the elements of each row in the matrix matrix are arranged in ascending order, we can use a binary search for each row to determine whether the target is in the row, so as to determine whether the target appears.

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;
    }
};

Second, there is no overlapping interval

435. Non-overlapping intervals - LeetCode https://leetcode.cn/problems/non-overlapping-intervals/?plan=data-structures&plan_progress=zz5yyb3

 Look at the solution:

Guess you like

Origin blog.csdn.net/m0_63309778/article/details/127060524