Leetcode Maximal Square
来源:互联网 发布:60数据库 编辑:程序博客网 时间:2024/06/05 21:04
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 01 0 1 1 11 1 1 1 11 0 0 1 0
Return 4.
Difficulty: Medium
动态规划, 状态转移方程:matrix[i][j] = min(matrix[i-1][j-1], min(matrix[i-1][j], matrix[i][j-1])) + 1;
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int row = matrix.size(); if(row == 0) return 0; int col = matrix[0].size(); if(col == 1 || row == 1){ for(int i = 0; i < row ;i++) for(int j = 0; j < col; j++) if(matrix[i][j] == '1') return 1; return 0; } int ans = 0; for(int i = 0; i < row ; i++) if(matrix[i][0] == '1') ans = 1; for(int i = 0; i < col ; i++) if(matrix[0][i] == '1') ans = 1; for(int i = 1; i < row ; i++) for(int j = 1; j < col; j++){ if((matrix[i][j-1] != '0' || matrix[i-1][j] != '0' || matrix[i-1][j-1] != '0') && matrix[i][j] == '1') matrix[i][j] = min(matrix[i-1][j-1], min(matrix[i-1][j], matrix[i][j-1])) + 1; if(matrix[i][j] - '0' > ans) ans = matrix[i][j] - '0'; } return ans * ans; }};
0 0
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- [leetcode] Maximal Square
- Maximal Square Leetcode Java
- [leetcode] Maximal Square
- leetcode 221: Maximal Square
- LeetCode Maximal Square
- Maximal Square - LeetCode 221
- #leetcode#Maximal Square
- LeetCode Maximal Square
- [LeetCode] Maximal Square
- [LeetCode]Maximal Square
- leetcode Maximal Square
- leetcode Maximal Square
- Leetcode 221 Maximal Square
- leetCode(27):Maximal Square
- [leetCode] Maximal Square
- Leetcode 221 Maximal Square
- POJ 1523 解题报告
- POJ 3176 Cow Bowling 动态规划
- HDU 5585 Numbers
- Android系列---JSON数据解析
- HDU 5590 ZYB's Biology
- Leetcode Maximal Square
- lambda
- HDU 5595 GTW likes math
- jQuery插件写法总结以及面向对象方式写法总结
- SpatialHadoop中空间索引系列之(四)空间格网索引实现
- 快速排序
- Git幕后的“故事”
- 何时使用static类(工具类)
- 高效操作字串的String Reference类