221. Maximal Square
来源:互联网 发布:华为手机网络连接错误 编辑:程序博客网 时间:2024/06/06 01:59
221 . Maximal Square
Difficulty: Medium
Given a 2D binary matrix filled with 0’s and 1’s, find the largest
square containing only 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.
解题思路
一道dp问题。其实我不是特别会做,但是基本思路就是如果当前位置是1,则计算它右下方三个点的最小的那个加一即可。也即是f(x,y) = min( f(x-1,y), f(x,y-1), f(x-1,y-1))+1。x,y为横纵坐标。
具体实现
class Solution {public: int min(int i,int j) { if(i > j) return j; return i; } int maxx(int i, int j) { if(i > j) return i; return j; } int maximalSquare(vector<vector<char>>& matrix) { if(matrix.size() == 0 || matrix[0].size() == 0) return 0; int n = matrix.size(); int m = matrix[0].size(); int arr[n][m]; int max = 0; for(int i = 0;i < n;i++) { if(matrix[i][0] == '1') { arr[i][0] = 1; max = 1; } else arr[i][0] = 0; } for(int i = 0;i < m;i++) { if(matrix[0][i] == '1') { arr[0][i] = 1; max = 1; } else arr[0][i] = 0; } for(int i = 1;i < n;i++) { for(int j = 1;j < m;j++) { if(matrix[i][j] == '0') arr[i][j] = 0; else { arr[i][j] = min(min(arr[i-1][j],arr[i][j-1]),arr[i-1][j-1]) + 1; max = maxx(arr[i][j],max); } } } return max*max; }};
0 0
- 221.Maximal Square
- [leetcode] 221.Maximal Square
- 【leetcode】221. Maximal Square
- [LeetCode]221. Maximal Square
- leetcode 221. Maximal Square
- 221. Maximal Square LeetCode
- 221. Maximal Square
- 221. Maximal Square
- LeetCode 221. Maximal Square
- 221. Maximal Square
- LeetCode *** 221. Maximal Square
- 221. Maximal Square
- LeetCode-221.Maximal Square
- leetcode.221. Maximal Square
- 221. Maximal Square
- Leetcode 221. Maximal Square
- 221. Maximal Square
- 221. Maximal Square
- 【noip2009提高组】 最优贸易 邻接表+SPFA
- 【BJOI】【欧拉函数】Euler
- Linux使用gnuplot将数据生成图
- 输入三角形的三边长(已确定能构成三角形),求三角形面积。
- videoView播放视频
- 221. Maximal Square
- 浏览器兼容性问题
- [Lintcode]Reorder List 重排链表
- Mac 下 查看 使用某端口的进程和关闭该进程的命令
- HTML5 表单验证的oninvalid事件
- Data structure 2016 17th
- The maximum number of apps for free development profiles has been reached.
- python学习-模块引入
- Android 开发技能图谱