zoukankan      html  css  js  c++  java
  • Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.

    For example, given the following matrix:

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

    Return 4.

    Runtime: 12ms.

     1 class Solution {
     2 public:
     3     int maximalSquare(vector<vector<char>>& matrix) {
     4         if(matrix.empty() || matrix[0].empty()) return 0;
     5         
     6         int row = matrix.size();
     7         int col = matrix[0].size();
     8         
     9         vector<vector<int> > dp(row, vector<int>(col, 0));
    10         int result = 0;
    11         for(int i = 0; i < row; i++)
    12             if(matrix[i][0] == '1'){
    13                 dp[i][0] = 1;
    14                 result = 1;
    15             }
    16             
    17         for(int j = 0; j < col; j++)
    18             if(matrix[0][j] == '1'){
    19                 dp[0][j] = 1;
    20                 result = 1;
    21             }
    22             
    23         for(int i = 1; i < row; i++){
    24             for(int j = 1; j < col; j++){
    25                 if(matrix[i][j] == '1'){
    26                     dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
    27                     result = max(result, dp[i][j]);
    28                 }
    29             }
    30         }
    31         return result * result;
    32     }
    33 };class Solution {
    34 public:
    35     int maximalSquare(vector<vector<char>>& matrix) {
    36         if(matrix.empty() || matrix[0].empty()) return 0;
    37         
    38         int row = matrix.size();
    39         int col = matrix[0].size();
    40         
    41         vector<vector<int> > dp(row, vector<int>(col, 0));
    42         int result = 0;
    43         for(int i = 0; i < row; i++)
    44             if(matrix[i][0] == '1'){
    45                 dp[i][0] = 1;
    46                 result = 1;
    47             }
    48             
    49         for(int j = 0; j < col; j++)
    50             if(matrix[0][j] == '1'){
    51                 dp[0][j] = 1;
    52                 result = 1;
    53             }
    54             
    55         for(int i = 1; i < row; i++){
    56             for(int j = 1; j < col; j++){
    57                 if(matrix[i][j] == '1'){
    58                     dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
    59                     result = max(result, dp[i][j]);
    60                 }
    61             }
    62         }
    63         return result * result;
    64     }
    65 };
  • 相关阅读:
    IOS开发教程--怎样使用点9图片
    Android studio 自己主动排版
    17 facade
    递归算法时间复杂度分析与改善
    __FUNCTION__, __LINE__ 有助于debug的宏定义
    表名在数据库中的存储大写和小写略解
    七夕节不撸代码你好意思说自己是程序员
    前端开发面试题收集(js部分)
    总体架构
    立即执行的匿名函数
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4845396.html
Copyright © 2011-2022 走看看