题目:

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

For example, given the following matrix: 

Return . 

解题思路:

  这种包含最大、最小等含优化的字眼时,一般都需要用到动态规划进行求解。本题求面积我们可以转化为求边长,由于是正方形,因此可以根据正方形的四个角的坐标写出动态规划的转移方程式(画一个图,从左上角推到右下角,很容易理解):

dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1; where matrix[i][j] == 1

根据此方程,就可以写出如下的代码:

代码展示:

 #include <iostream>
#include <vector>
#include <cstring>
using namespace std; //dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1;
//where matrix[i][j] == 1
int maximalSquare(vector<vector<char>>& matrix)
{
if (matrix.empty())
return ; int rows = matrix.size();//行数
int cols = matrix[].size(); //列数 vector<vector<int> > dp(rows+, vector<int>(cols+, ));
/*
0 0 0 0 0 0
0 1 0 1 0 0
0 1 0 1 1 1
0 1 1 1 1 1
0 1 0 0 1 0
*/
int result = ; //return result for (int i = ; i < rows; i ++) {
for (int j = ; j < cols; j ++) {
if (matrix[i][j] == '') {
int temp = min(dp[i][j], dp[i][j+]);
dp[i+][j+] = min(temp, dp[i+][j]) + ;
}
else
dp[i+][j+] = ; result = max(result, dp[i+][j+]);
}
}
return result * result; //get the area of square;
} // int main()
// {
// char *ch1 = "00000";
// char *ch2 = "00000";
// char *ch3 = "00000";
// char *ch4 = "00000";
// vector<char> veccol1(ch1, ch1 + strlen(ch1));
// vector<char> veccol2(ch2, ch2 + strlen(ch2));
// vector<char> veccol3(ch3, ch3 + strlen(ch3));
// vector<char> veccol4(ch4, ch4 + strlen(ch4));
//
// vector<vector<char> > vecrow;
// vecrow.push_back(veccol1);
// vecrow.push_back(veccol2);
// vecrow.push_back(veccol3);
// vecrow.push_back(veccol4);
//
// vector<vector<char> > vec;
// cout << maximalSquare(vec);
// return 0;
// }

LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium的更多相关文章

  1. LeetCode:搜索二维矩阵【74】

    LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...

  2. 01二维矩阵中最大全为1的正方形maxSquare——经典DP问题(二维)

    在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...

  3. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  4. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  5. Leetcode 240.搜索二维矩阵II

    搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...

  6. Java实现 LeetCode 240 搜索二维矩阵 II(二)

    240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...

  7. Java实现 LeetCode 74 搜索二维矩阵

    74. 搜索二维矩阵 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: ...

  8. leetcode 240搜索二维矩阵

    /** 正常的二维搜索估计要超时,本题沿着对角线搜索,然后找到第一个大于目标数字的坐标(x,y)然后搜索(>x,<y)(<x,>y)子区域: 矩阵size() 为m,n:当i& ...

  9. leetcode 74 搜索二维矩阵 java

    题目: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: mat ...

随机推荐

  1. clearing & settlement

    http://blog.donews.com/tianshun/archive/2013/07/ http://wenku.baidu.com/view/e5a736e3e53a580217fcfe1 ...

  2. Redis教程(七):Key操作命令详解

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/134.html?1455807040 一.概述: 在该系列的前几篇博客中, ...

  3. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  4. Java日期与时间的处理/Date,String,Calendar转换

    public class Demo01 { //Java中Date类和Calendar简介 public static void main(String[] args) { long now=Syst ...

  5. Language

    TODO Java(jdk8),Scala,Spring,Spark,Python,Linux,C,Netty,HttpClient,Hadoop YARN,RaspberryPi Jetty,Tom ...

  6. html页面禁止自动填充浏览器记住的密码

    现在的浏览器功能越来越强大,比如Chrome浏览器,在一个系统login的时候我们一般会记住密码,那么在整个系统中,浏览器一旦遇到 type="password"的控件,就会把密码 ...

  7. iOS 渐变提示。Labe自动换行

    UILabel *noticeLabel = [[UILabel alloc]init]; noticeLabel.text = @"当前账号已失效果,已清除当前账号已失效果,已清除当前账号 ...

  8. iOS crash 异常捕获

    // //  UncaughtExceptionHandler.m //  UncaughtExceptions // //  Created by Matt Gallagher on 2010/05 ...

  9. Lowest Common Ancestor of Two Nodes in a Binary Tree

    Reference: http://blog.csdn.net/v_july_v/article/details/18312089  http://leetcode.com/2011/07/lowes ...

  10. Huxley 是一个用于Web应用 UI 测试的工具

    Huxley 是一个用于Web应用 UI 测试的工具,由  Pete Hunt 和 Maykel Loomans 用 Python 开发. UI 测试比较令人头疼. UI测试不好写,而且很容易失效: ...