当输入量很大时,如何在线判断调试?

时间:2017-04-09 11:14:53

标签: java debugging testing

今天我花了几个小时在网上判断调试程序 -

问题:

给定一个填充0和1的二进制矩阵,找到只包含1的最大矩形并返回其区域。

例如,给定以下矩阵:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

LeetCode

我的代码是: 它通过了60/66的测试用例,我不知道错误在哪里。我希望你能给我一个关于我的错误的好答案,以及当输入很大时我如何调试。

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix==null||matrix.length==0) return 0;
        short[][] smatrix=new short[matrix.length+1][];
        smatrix[matrix.length]=new short[matrix[0].length];
        for(int i=0;i<matrix.length;i++){
            smatrix[i]=new short[matrix[i].length];
            for(int j=0;j<matrix[i].length;j++)
                smatrix[i][j]=(short) (matrix[i][j]-'0');
        }
        for(int j=0;j<matrix[0].length;j++)
            smatrix[matrix.length][j]=0;

        //travel ever row 
        int max=-1;
        for(int i=0;i<smatrix.length;i++){
            count(smatrix[i]);
            for(int j=0;j<smatrix[i].length;j++)
                max=smatrix[i][j]>max?smatrix[i][j]:max;
        }
        if(smatrix.length==1) return max;


        //travel every row and each row to the tail-1
       for(int i=0;i<smatrix.length-1;i++){
            andTwoRows(smatrix[i],smatrix[i+1]);
            for(int j=i+1;j<smatrix.length;j++){
                max=andTwoRows(max,j-i,smatrix[i],smatrix[j]);
                if(!haveOne(smatrix[i])) break;
            }
       }
       return max;
    }
    private void andTwoRows(short[] head,short[] cur){
        short[] onehead=new short[head.length];
        short[] onecur=new short[cur.length];
        for(int i=0;i<head.length;i++){
            if(head[i]>0)
                for(int j=0;j<head[i];j++) onehead[i+j]=1;
            if(cur[i]>0)
                for(int j=0;j<cur[i];j++) onecur[i+j]=1;
        }
        for(int i=0;i<head.length;i++)
            head[i]=(short) (onehead[i]&onecur[i]);
        count(head);
    }

    private void count(short[] rows){
        for(int i=0,k=-1;i<rows.length;i++){
            if(rows[i]==0) k=-1;
            if(k<0&&rows[i]==1) {k=i;continue;}
            if(k>=0) {rows[k]+=rows[i];rows[i]=0;}
        }
    }
    private int andTwoRows(int max,int h,short[] head,short[] cur){
        short[] res=new short[head.length];
        System.arraycopy(head,0,res,0,head.length);
        andTwoRows(res,cur);

        for(int i=0;i<head.length;i++){
            if(res[i]<head[i]){
                max=h*head[i]>max?h*head[i]:max;
                head[i]=res[i];
            }
        }
        return max;
    }
    private boolean haveOne(short[] row){
        for(short tmp:row)
            if(tmp>0) return true;
        return false;
    }
}

测试用例:

["1111111111111101001111111100111011111111",
 "1111011011111111101101111101111111111111",
 "0111101011111101101101101111111111111111",
 "0101101011111111111101111111010110111111",
 "1111111111110111110110010111111111111111",
 "1111111110110101111111111101011111101111",
 "0110110101110011111111111111110111110101",
 "0111111111111100111111100110011011010101",
 "1111011110111111111011011011110101101011",
 "1111111110111111111101101101110111101111",
 "1110110011111111111100111111111111111111",
 "1011111110111101111001110111111111111111",
 "0110111111111111101111110111011111011111",
 "1111111111111111011111111111110111111011",
 "1111100111111110101100111111111111101111",
 "1111101111111110111111011111111111011111",
 "1111101111111111111111011001111110011111",
 "1111110111111111011111111111110111110111",
 "1011111111111111010111110010111110111111",
 "1111110011111111111110111111111111111011",
 "1111111111111111110111011111011111011011",
 "1100011011111111111111011111011111111111",
 "1111101011111111111101100101110011111111",
 "1110010111111111111011011110111101111101",
 "1111111111111101101111111111101111111111",
 "1111111011111101111011111111111110111111",
 "1110011111111110111011111111110111110111",
 "1111111111111100111111010111111111110111",
 "1111111111111111111111000111111111011101",
 "1111110111111111111111111101100111011011",
 "1111011011111101101101111110111111101111",
 "1111111111011111111111111111111111111111",
 "1111111111111111111111111111111111111111",
 "1100011111111110111111111111101111111011",
 "1111101111111101111010111111111111111111",
 "0111111111110011111111110101011011111111",
 "1011011111101111111111101111111111110011",
 "1010111111111111111111111111111110011111",
 "0111101111011111111111111111110111111111",
 "0111111011111111011101111011101111111111",
 "0111111111110101111011111101011001111011",
 "1111111111111011110111111101111111101110",
 "1111101111111100111111111110111111001111",
 "1101101111110101111101111111100111010100",
 "0110111111100111110010111110111011011101"]

我的输出:96 预期:114

test case

因为测试用例输入很大。我想要调试时该怎么做。

0 个答案:

没有答案