240. 搜索二维矩阵 II

目录

  • 题目
  • 解法
  • [for (const auto& row: matrix) {](#for (const auto& row: matrix) {)

题目

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

每行的元素从左到右升序排列。

每列的元素从上到下升序排列。

解法

cpp 复制代码
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;
    }
};

对每一行进行二分查找

for (const auto& row: matrix) {

对矩阵遍历就是遍历每一行

相关推荐
达子6664 小时前
RUST 图解 第 3 章:通用编程概念(Common Programming Concepts)
开发语言·算法·rust
夏幻灵5 小时前
每日一题:LeetCode 165:比较版本号
算法·leetcode·职场和发展
道尔柯南5 小时前
C++ 面向对象编程入门:类和对象(上)
开发语言·c++·算法
Navigator_Z5 小时前
LeetCode //C - 1288. Remove Covered Intervals
c语言·算法·leetcode
码爸5 小时前
决策树介绍与示例
python·算法·机器学习
动词ing6 小时前
【题目练习】字符串相乘
算法
0+1116 小时前
算法 --链表
数据结构·算法·leetcode·链表
aqiu1111116 小时前
【C++算法打怪专栏】C++ `unordered_set` 常用初始化与插入方法总结
开发语言·c++·算法
Terra.K7 小时前
高精度阶乘求和模板深度解析
开发语言·c++·算法
Peregrine97 小时前
C++ - > 类和对象
java·c++·算法