O(m + n) solution


  • 0
    L
    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            if (matrix.empty() || matrix.front().empty()) return false;
            int row = 0, col = matrix.front().size() - 1;
            while (row < matrix.size() && col >= 0) {
                if (matrix[row][col] == target) return true;
                else if (matrix[row][col] > target) col--;
                else row++;
            }
            return false;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.