0ms Java Solution. Bit shift instead of division


  • 0
    G
        public boolean searchMatrix(int[][] matrix, int target) {
            int len = matrix.length, len2;
            if(matrix==null || len==0 || (len2=matrix[0].length)==0 || matrix[0][0]>target)
                return false;
    
            int l=0, r=len-1, p=r>>>1;
            while(l<=r){
                int v = matrix[p][0];
                if(v==target)
                    return true;
                if(v>target)
                    r = p-1;
                else
                    l = p+1;
                p = (l+r)>>>1;
            }
            
            l=0; r=len2-1;
            while(l<=r){
                int p2 = (l+r)>>>1;
                int v = matrix[p][p2];
                if(v==target)
                    return true;
                if(v>target)
                    r = p2-1;
                else
                    l = p2+1;
            }
    
            return false;
        }
    

Log in to reply
 

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