C++ divide & conquer (30.17%)


  • 0
    class Solution { 
    public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();
        return find(matrix, target, 0,n-1, 0 , m -1);
    }
        bool find(vector<vector<int>>& matrix, int target, int left, int right, int up, int down){
        if(left > right || up > down || target < matrix[up][left] || target > matrix[down][right]) return false;
        int row_mid = up + (down - up)/2;
        int col_mid = left + (right - left)/2;
        if(matrix[row_mid][col_mid] < target){
            return find(matrix, target,col_mid+1, right ,row_mid+1, down) || find(matrix, target,left,col_mid,row_mid+1,down) || find(matrix, target,col_mid+1, right, up, row_mid);
        }else if(matrix[row_mid][col_mid] > target){
            return find(matrix, target,left, col_mid-1 ,up, row_mid-1) || find(matrix, target,left,col_mid-1,row_mid,down) || find(matrix, target,col_mid, right, up, row_mid-1);
        }else{
            return true;
        }
    } 
    };

  • 0
    B

    @jasonshieh Your code could really use some clean up.


Log in to reply
 

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