LeetCode面试题 10.09. 排序矩阵查找

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

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

        if (!n)
        {
            return false;
        }

        int row = m - 1;
        int col = 0;

        while (row >= 0 && col <= n - 1)
        {
            int temp = matrix[row][col];
            if (temp == target)
            {
                return true;
            }
            else if (temp > target)
            {
                row--;
            }
            else if (temp < target)
            {
                col++;
            }
        }

        return false;
    }
};

你可能感兴趣的:(算法)