zoukankan      html  css  js  c++  java
  • leetCode(27):Maximal Square 分类: leetCode 2015-07-02 09:05 152人阅读 评论(0) 收藏

    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.


        动态规划

    class Solution {
    public:
        int maximalSquare(vector<vector<char>>& matrix) {
            if(matrix.empty() || matrix[0].empty()) return 0;
            int maxSize = 0;        
            vector<vector<int>> m(matrix.size(), vector<int>(matrix[0].size(), 0));
            for(int r = 0 ; r < m.size() ; ++r) {
                for(int c = 0 ; c < m[0].size() ; ++c) {
                    m[r][c] = matrix[r][c]-'0';
                    if(r > 0 && c > 0 && m[r][c] == 1) {
                        m[r][c] += min(m[r-1][c], min(m[r][c-1], m[r-1][c-1]));
                    }
                    maxSize = max(maxSize, m[r][c]);
                }
            }
            return maxSize*maxSize;
    
        }
    };


  • 相关阅读:
    3185 队列练习 1 3186 队列练习 2
    1063 合并果子
    堆排序
    奇怪的电梯
    3411 洪水
    2010 求后序遍历
    1729 单词查找树
    3137 栈练习1
    2821 天使之城
    括弧匹配检验(check.cpp)
  • 原文地址:https://www.cnblogs.com/zclzqbx/p/4687079.html
Copyright © 2011-2022 走看看