leetcode 240 two-dimensional matrix search

 

/ * * 
The normal two-dimensional search is estimated to be a timeout, the title search along the diagonal, then find the first coordinate is greater than the target number of (x, y) and Search (> x, <y) ( <x,> y ) sub-region; 
time = n, j = n if i> i updated when m = = m, j empathy>;; matrix size () as m, n j and i at the same time when n, when m is not find the target the number of return; 
* * / 

class Solution {
 public :
     BOOL searchMatrix (Vector <Vector < int >> Matrix &, int target) {
         int m = matrix.size ();
         IF (m == 0 ) return  to false ;
         int n- Matrix = [ 0 ] .size ();
         IF (n-== 0 ) return  to false ;
         int x=0,y=0;
        //cout<<m<<","<<n<<endl;
        for(int k=0;k<max(m,n);k++){
            //cout<<matrix[x][y]<<endl;
            if(matrix[x][y]<target){
                x=x<m-1?(x+1):m-1;
                y=y<n-1?(y+1):n-1;
                continue;
            }else{
                if(matrix[x][y]==target)return true;
                for(int i=x;i<m;i++)
                    for(int j=0;j<y;j++)
                        if(matrix[i][j]==target) return true;
                for(int i=0;i<x;i++)
                    for(int j=y;j<n;j++)
                        if(matrix[i][j]==target) return true;
                return false;
            }
        }
        return false;
    }
};

 

Guess you like

Origin www.cnblogs.com/joelwang/p/10929959.html