• leetcode 221.最大正方形


    题目:

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

    分析:

    在动态规划的过程中,判断当前位置是否为0,如果是就直接写入0,为1进行判断,如果左边和上边的数字不相等,当前位置就是较小的数字加一,如果相等,与左上数字比较,取较小的数字加一。

    代码:

     1 //11ms 75%
     2 class Solution {
     3     public int maximalSquare(char[][] matrix) {
     4         if(matrix.length<1)
     5             return 0;
     6         int max=0;
     7         int lex=matrix[0].length,ley=matrix.length;
     8         int[][] con=new int[ley][lex];
     9         for(int n=0;n<ley;++n) {
    10             con[n][0]=matrix[n][0]-'0';
    11             max=max>con[n][0]?max:con[n][0];
    12         }
    13         for(int n=0;n<lex;++n) {
    14             con[0][n]=matrix[0][n]-'0';
    15             max=max>con[0][n]?max:con[0][n];
    16         }
    17         for(int n=1;n<ley;++n)
    18             for(int m=1;m<lex;++m) {
    19                 if(matrix[n][m]=='0')
    20                     con[n][m]=0;
    21                 else
    22                     if(con[n-1][m]==con[n][m-1]) {
    23                         if(con[n-1][m-1]>con[n-1][m])
    24                             con[n][m]=con[n-1][m]+1;
    25                         else
    26                             con[n][m]=con[n-1][m-1]+1;
    27                     }
    28                     else
    29                         if(con[n-1][m]>con[n][m-1])
    30                             con[n][m]=con[n][m-1]+1;
    31                         else
    32                             con[n][m]=con[n-1][m]+1;
    33                 max=max>con[n][m]*con[n][m]?max:con[n][m]*con[n][m];
    34             }
    35         return max;
    36     }
    37 }
  • 相关阅读:
    python bif 如何自学
    python萌新应知应会
    Animation
    响应式布局
    浏览器兼容
    HTML基础
    SublimeText 3 Emmet Hot Keys
    Web大前端环境搭建
    Sublime Text 运行js
    bash脚本编程基础
  • 原文地址:https://www.cnblogs.com/CHAHA123/p/10643102.html
Copyright © 2020-2023  润新知