题目:

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. 学习WPF——WPF布局——初识布局容器

    StackPanel堆叠布局 StackPanel是简单布局方式之一,可以很方便的进行纵向布局和横向布局 StackPanel默认是纵向布局的 <Window x:Class="Wpf ...

  2. NBIbatis web/winform框架

    Web框架 调用Bussiness和DataAccess可参考微信框架的后台. Pages/Meeting/MeetingList.aspx Pages/Meeting/MeetingEdit.asp ...

  3. [芯片][MPU6050] MPU60X0的DMP相关链接

    标题:发个自己做的UD分解+强跟踪卡尔曼滤波做的双轴姿态测量 链接:http://www.amobbs.com/thread-5511854-1-1.html 关键词:UD分解+强跟踪卡尔曼滤波,采用 ...

  4. [ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]

    MightyHorse is playing a music game called osu!. After playing for several months, MightyHorse disco ...

  5. [JS] HTML QQ分享界面js代码

    @ - @ :可以自己指定分享内容等....内含JS脚本 <html> <head> <title></title> <script type=& ...

  6. 让VS默认以管理员身份运行

    In Windows 8 & 10, you have to right-click devenv.exe and select "Troubleshoot compatibilit ...

  7. servlet 读取web.xml参数

    1初始化参数init-param init-param是配置在web.xml的<servlet>标签里的,也就是说,是归该servlet单独所有的. 实例 <servlet> ...

  8. Linux下的NFS配置(转)

    http://rubyer.me/blog/1682/ 遇到的问题: 1.reason given by server: Permission denied 在服务器的/etc/export配置文件中 ...

  9. 用distinct在MySQL中查询多条不重复记录值[转]

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...

  10. paip.自定义java 泛型类与泛型方法的实现总结

    paip.自定义java 泛型类与泛型方法的实现总结 ============泛型方法     public static <atiType,retType> retType reduce ...