Simple Straight Forward Solution without binary search or recursion


  • 0
    Y
    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            if(matrix.size()==0 || matrix[0].size()==0)
                return false;
            
            bool resultBoolean = false;
    
            int matSize= matrix[0].size();
            for(int i=0; i < matrix.size(); ++i){
                if(matrix[i][matSize-1]>=target){
                    // search in the ith vector if the target is in there or not
                    for(vector<int>::iterator it= matrix[i].begin(); it!=matrix[i].end(); ++it){
                        if (*it==target){
                            resultBoolean= true;
                        }
                    }
                    cout << endl;
                }
            }
    
            return resultBoolean;
    
        }
    };
    

Log in to reply
 

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