Table of contents
First, search the two-dimensional matrix
Second, there is no overlapping interval
First, search the two-dimensional matrix
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