1. 程式人生 > 其它 >分享一個dotnet自動釋出的指令碼

分享一個dotnet自動釋出的指令碼

在一個由 '0' 和 '1' 組成的二維矩陣內,找到只包含 '1' 的最大正方形,並返回其面積。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/maximal-square
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

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

            }
        }
        return result * result;

    }
}
心之所向,素履以往 生如逆旅,一葦以航