[Leetcode] #221 Maximal Square (DP)
来源:互联网 发布:网络推广员的发展方向 编辑:程序博客网 时间:2024/05/21 21:40
DIscription:
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 01 0 1 1 11 1 1 1 11 0 0 1 0Return 4.
Solution:
int maximalSquare(vector<vector<char>>& matrix) {if (matrix.empty())return 0;int rows = matrix.size(), cols = matrix[0].size();vector<vector<int>> len(rows, vector<int>(cols, 0));int maxlen = 0;for (int i = 0; i < rows; i++){len[i][0] = matrix[i][0] - '0';if (len[i][0] == 1)maxlen = 1;}for (int i = 0; i < cols; i++){len[0][i] = matrix[0][i] - '0';if (len[0][i] == 1)maxlen = 1;}for (int i = 1; i < rows; i++){for (int j = 1; j < cols; j++){if (matrix[i][j] == '0')len[i][j] = 0;else{len[i][j] = min(min(len[i - 1][j], len[i][j - 1]), len[i - 1][j - 1]) + 1;if (len[i][j]>maxlen)maxlen = len[i][j];}}}return maxlen*maxlen;}
1 0
- [Leetcode] #221 Maximal Square (DP)
- LeetCode Maximal Square DP
- leetcode----DP----Maximal Square
- Leetcode 221 - Maximal Square(dp)
- leetcode -- Maximal Square -- DP重点
- leetcode 221: Maximal Square
- Maximal Square - LeetCode 221
- Leetcode 221 Maximal Square
- Leetcode 221 Maximal Square
- LeetCode#221 Maximal Square
- [leetcode 221]Maximal Square
- LeetCode(221) Maximal Square
- leetcode 221: Maximal Square
- [LeetCode 221] Maximal Square
- LeetCode 221: Maximal Square
- LeetCode 221 Maximal Square
- leetcode 221 Maximal Square
- LeetCode 221 Maximal Square
- [项目实训]Python正则表达式和Re库
- StringBuffer类操作字符串
- LDA主题模型、Word2Vec
- 动态规划5
- 数据库中的范式
- [Leetcode] #221 Maximal Square (DP)
- 上传文件到github
- 编写python爬虫 获取中华英才网全网工资数据
- 第一周(2):myflag开发细化
- 把一个字符串转换成整数
- linux的getrlimit()和setrlimit()
- 两台路由器桥接
- 什么是LXC
- 第十四届北京师范大学程序设计竞赛 [6/11]