C++ | Leetcode C++题解之第74题搜索二维矩阵

题目:

题解:

cpp 复制代码
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size(), n = matrix[0].size();
        int low = 0, high = m * n - 1;
        while (low <= high) {
            int mid = (high - low) / 2 + low;
            int x = matrix[mid / n][mid % n];
            if (x < target) {
                low = mid + 1;
            } else if (x > target) {
                high = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }
};
相关推荐
潼心1412o2 小时前
C++初阶(长期更新)第5讲:类和对象(中)
开发语言·c++
weixin_307779133 小时前
C++代码实现MATLAB中的normalize函数功能
开发语言·c++·算法·matlab
(Charon)3 小时前
【C++面试】手写String类:同时实现拷贝构造与移动构造
c++·算法
程序员老陆3 小时前
通过std::unique_ptr初始化std::shared_ptr
开发语言·c++
CoderYanger3 小时前
A.每日一题:1614. 括号的最大嵌套深度
java·程序人生·算法·leetcode·面试·学习方法
Xin7704 小时前
LeetCode 230. 二叉搜索树中第 K 小的元素(迭代中序遍历 + 栈)
leetcode
西西弗Sisyphus5 小时前
Qt6 实现 会动的界面像物理世界一样
c++·qt
UIU1145 小时前
头文件可以防什么错?运用头文件做声明
c++·学习·c#
Navigator_Z5 小时前
LeetCode //C - 1283. Find the Smallest Divisor Given a Threshold
c语言·算法·leetcode
by209995 小时前
list:迭代器与资源管理实现的综合叙述(下)
数据结构·c++·笔记·list