Simple, efficient and clean C++ solution


  • 0
    R
    class Solution {
    public:
     
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
                int i = 0;
                int j = matrix[0].size()-1;
                while(i < matrix.size() && j >= 0){
                    if(matrix[i][j] == target)
                        return true;
                    
                    if(matrix[i][j] > target)
                        j--;
                    else
                        i++;
                }
                return false;
        }
    };

Log in to reply
 

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