解题思路

dp

提交代码

class Solution {
    public int maximalSquare(char[][] matrix) {
    	if(matrix==null||matrix.length==0||matrix[0].length==0)
    		return 0;
    	
    	int max=0;
        int[] preRow=new int[matrix[0].length];
        int[] curRow=new int[matrix[0].length];
    	for(int i=0;i<matrix.length;i++) {
        	for(int j=0;j<matrix[0].length;j++) {
        		if(matrix[i][j]=='0') {
        			curRow[j]=0;
        			continue;
        		}
        		
        		if(j==0) curRow[0]=1;
        		else
        		curRow[j]=Math.min(Math.min(curRow[j-1],preRow[j]), preRow[j-1])+1;

        		max=curRow[j]>max?curRow[j]:max;
        	}
        	for(int k=0;k<curRow.length;k++)
        		preRow[k]=curRow[k];
    	}
    	return max*max;
    }
}

运行结果

【leetcode】221(Medium)Maximal Square

相关文章: