Leetcode—74. 搜索二维矩阵【中等】

2024每日刷题(149)

Leetcode---74. 搜索二维矩阵

实现代码

cpp 复制代码
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();

        int l = 0;
        int r = m * n;
        int mid = -1;
        // 左闭右开
        while(l < r) {
            mid = (r - l) / 2 + l;
            if(matrix[mid / n][mid % n] == target) {
                return true;
            }
            if(matrix[mid / n][mid % n] < target) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return false;
    }
};

运行结果

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
雪域迷影9 小时前
C++中编写UT单元测试用例时如何mock非虚函数?
开发语言·c++·测试用例·gmock·cpp-stub开源项目
sheeta199811 小时前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode
是小胡嘛14 小时前
C++之Any类的模拟实现
linux·开发语言·c++
Want59516 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
lingggggaaaa16 小时前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
phdsky17 小时前
【设计模式】建造者模式
c++·设计模式·建造者模式
H_-H17 小时前
关于const应用与const中的c++陷阱
c++
coderxiaohan17 小时前
【C++】多态
开发语言·c++
gfdhy17 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
百***060117 小时前
SpringMVC 请求参数接收
前端·javascript·算法