Java short easy to understand


  • 0
    Y
    public boolean searchMatrix(int[][] matrix, int target) {
            if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
            for(int i=0; i<matrix.length; i++)
            {
                if(matrix[i][0] <= target && target <= matrix[i][matrix[i].length-1])
                {
                    int cf=0, cl=matrix[i].length-1;
                    while(cf <= cl)
                    {
                        int m = cf + (cl - cf)/2;
                        if(matrix[i][m] == target )
                            return true;
                        else if(matrix[i][m] > target)
                            cl = m-1;
                        else if(matrix[i][m] < target)
                            cf = m+1;
                    }        
                }else if(matrix[i][0] > target)
                    break;
            }
            return false;
        }
    

Log in to reply
 

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