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;
    }
};
相关推荐
ShineWinsu4 小时前
对于C++:类和对象的解析—下(第二部分)
c++·面试·笔试·对象··工作·stati
如何原谅奋力过但无声4 小时前
【力扣-Python-滑动窗口经典题】567.字符串的排列 | 424.替换后的最长重复字符 | 76.最小覆盖子串
算法·leetcode
BHXDML5 小时前
第七章:类与对象(c++)
开发语言·c++
52Hz1185 小时前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
yyf198905256 小时前
C++ 跨平台开发的挑战与应对策略
c++
iAkuya6 小时前
(leetcode)力扣100 二叉搜索树种第K小的元素(中序遍历||记录子树的节点数)
算法·leetcode·职场和发展
又见野草6 小时前
C++类和对象(中)
开发语言·c++
Remember_9937 小时前
【LeetCode精选算法】滑动窗口专题二
java·开发语言·数据结构·算法·leetcode
hellokandy8 小时前
C++ 如何知道程序最多可以申请多少内存
c++·vector·cin·cout
圣保罗的大教堂8 小时前
leetcode 1895. 最大的幻方 中等
leetcode