跳转至

搜索二维矩阵⚓︎

Leetcode题目链接

描述⚓︎

详见中文题目链接

解答⚓︎

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size(), n = matrix[0].size();
        int l = -1, r = m * n;
        while (l + 1 < r) {
            int mid = l + r >> 1;
            if (matrix[mid / n][mid % n] >= target) r = mid;
            else l = mid;
        }
        if (r < m * n && matrix[r / n][r % n] == target) return true;
        return false;
    }
};