Leetcode 240 Solution

This article provides solution to leetcode question 240 (search-a-2d-matrix-ii)

https://leetcode.com/problems/search-a-2d-matrix-ii

Solution

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();

        if (m == 0)
            return false;

        int n = matrix[0].size();

        int i = m - 1;
        int j = 0;

        while (i >= 0 && j < n)
        {
            if (matrix[i][j] > target)
                i--;
            else if (matrix[i][j] < target)
                j++;
            else
                return true;
        }

        return false;
    }
};