程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

python面试大全(3)

python前言(0)

标签  

python面试(2)

python(0)

日期归档  

Leetcode 第221题:Maximal Square--最大正方形(C++、Python)

发布于2019-08-07 12:33     阅读(705)     评论(0)     点赞(0)     收藏(5)


题目地址:Maximal Square


题目简介:

在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

示例:

  1. Input:
  2. 1 0 1 0 0
  3. 1 0 1 1 1
  4. 1 1 1 1 1
  5. 1 0 0 1 0
  6. Output: 4

输出4的原因是第2行和第3行中组成了2x2的正方形。


题目分析:

1、简单法

正方形的左上角也是正方形,正方形的最小边长为1。所以只要碰到'1',按照正方行对角线扩展,边长每增加1,对应轮廓增加1。从左上到右下,只要保证每次扩展的边界没有'0',便可以增加轮廓。

C++:

  1. class Solution {
  2. public:
  3. int maximalSquare(vector<vector<char>>& matrix) {
  4. if (matrix.empty() || matrix[0].empty())
  5. return 0;
  6. int ans = 0, row = matrix.size(), col = matrix[0].size();
  7. for (int i = 0; i < row;i++)
  8. for (int j = 0; j < col; j++)
  9. if (matrix[i][j] == '1')
  10. ans = max(ans, helper(i, j, matrix));
  11. return ans;
  12. }
  13. int helper(int x, int y, vector<vector<char>>& matrix){
  14. int circle = 1;
  15. bool flag = true;
  16. int row = matrix.size(), col = matrix[0].size();
  17. while(flag)
  18. {
  19. circle++;
  20. if (x + circle <= row && y + circle <= col)
  21. {
  22. for (int i = x; i < x + circle;i++)
  23. {
  24. if(matrix[i][y + circle - 1] == '0')
  25. {
  26. flag = false;
  27. circle--;
  28. return pow(circle, 2);
  29. }
  30. }
  31. for (int j = y; j < y + circle;j++)
  32. {
  33. if(matrix[x + circle - 1][j] == '0')
  34. {
  35. flag = false;
  36. circle--;
  37. return pow(circle, 2);
  38. }
  39. }
  40. }
  41. else
  42. {
  43. flag = false;
  44. circle--;
  45. }
  46. }
  47. return pow(circle, 2);
  48. }
  49. };

Python:

  1. class Solution:
  2. def maximalSquare(self, matrix: List[List[str]]) -> int:
  3. if len(matrix) == 0 or len(matrix[0]) == 0:
  4. return 0
  5. ans = 0
  6. row = len(matrix)
  7. col = len(matrix[0])
  8. def helper(x, y):
  9. circle = 1
  10. # flag = True
  11. while True:
  12. circle += 1
  13. if x + circle <= row and y + circle <= col:
  14. for i in range(x , x + circle):
  15. if matrix[i][y + circle - 1] == '0':
  16. # flag = False
  17. circle -= 1
  18. return pow(circle, 2)
  19. for j in range(y, y + circle):
  20. if matrix[x + circle - 1][j] == '0':
  21. # flag = False
  22. circle -= 1
  23. return pow(circle, 2)
  24. else:
  25. # flag = False
  26. circle -= 1
  27. return pow(circle, 2)
  28. for i in range(row):
  29. for j in range(col):
  30. if matrix[i][j] == '1':
  31. ans = max(ans, helper(i, j))
  32. return ans

2、递归

考虑这样一个例子:

  1. 1 1 1
  2. 1 1 1
  3. 1 1 1

这个例子中,假设已经知道除了不加右下角的1之外的所有最大面积。那么怎么得到包含右下角1的最大面积呢?首先,根据上面可知,右下角邻接的3个左、上、左上所能组成的均是一个2x2的正方形。那么,当左边的正方形被破坏呢?

  1. 1 1 1
  2. 1 1 1
  3. 0 1 1

其能组成的面积,要依赖左边点的面积。此时左边最大为1,组成结果为2x2。那么此时再将上面两个1破坏呢?

  1. 1 1 1
  2. 1 0 0
  3. 0 1 1

这个时候,左上和上的最大面积仅为0,所以,这时也组成不了2x2的面积,只能为1x1。总结一个规律就是:

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

C++:

  1. class Solution {
  2. public:
  3. int maximalSquare(vector<vector<char>>& matrix) {
  4. if (matrix.empty())
  5. return 0;
  6. int row = matrix.size(), col = matrix[0].size(), ans = 0;
  7. vector<vector<int>> dp(row, vector<int>(col, 0));
  8. for (int i = 0; i < row; i++)
  9. {
  10. for (int j = 0; j < col; j++)
  11. {
  12. if (!i || !j || matrix[i][j] == '0')
  13. {
  14. dp[i][j] = matrix[i][j] - '0';
  15. }
  16. else
  17. {
  18. dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
  19. }
  20. ans = max(dp[i][j], ans);
  21. }
  22. }
  23. return pow(ans, 2);
  24. }
  25. };

Python:

  1. class Solution:
  2. def maximalSquare(self, matrix: List[List[str]]) -> int:
  3. if len(matrix) == 0 or len(matrix[0]) == 0:
  4. return 0
  5. ans = 0
  6. row = len(matrix)
  7. col = len(matrix[0])
  8. dp = [[0 for j in range(col)] for i in range(row)]
  9. for i in range(row):
  10. for j in range(col):
  11. if (not i or not j or matrix[i][j] == '0'):
  12. dp[i][j] = ord(matrix[i][j]) - ord('0')
  13. else:
  14. dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1
  15. ans = max(dp[i][j], ans)
  16. return pow(ans, 2)

 

 

 



所属网站分类: 技术文章 > 博客

作者:骷髅无悔

链接:https://www.pythonheidong.com/blog/article/10836/d6643fe60ad845a88b1b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)