zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Exception和Error有什么区别?
    网络流量劫持的含义
    安全术语:
    加载相关
    10、接到任务后的整个测试前准备流程总结
    fiddler工具栏数据解释
    HTTP的请求头标签 If-Modified-Since
    VueStudyDemo
    Vue从入门到放弃
    TypeScript初体验
  • 原文地址:https://www.cnblogs.com/CHAHA123/p/10643102.html
Copyright © 2011-2022 走看看