题目:

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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 4.

题目大意:
给一个只包含0和1的矩阵,求里面值都为1的一个最大方阵,返回方阵面积

思路:参考了这里
以递推的方式求出矩阵每个点为方阵右下角时,该方阵的层数
递推公式:
如果当前点为 0,残缺的方阵,层数为0, level(i,j) = 0
如果当前点为 1,层数等于左、上、左上方阵最小的一个层数加1,level(i, j) = min(level(i-1,j), level(i,j-1), level(i-1,j-1)) + 1
代码

#define min(a, b) ((a) < (b) ? (a) : (b))
#define level(i, j) level[(i) * matrixColSize + (j)]    //方便代码阅读,一维数组转二维数组int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize) {int *level = NULL;  /* 矩阵上每个点为方阵右下角时方阵的层数 */int i, j;int maxLevel = 0;  if(!matrix || !matrixRowSize || !matrixColSize)return 0;level = (int *)malloc(matrixRowSize * matrixColSize * sizeof(int));if(!level)return 0;/* 初始化第一行和第一列为方阵右下角时的层数 */for(j = 0; j < matrixColSize; j++){level(0, j) = matrix[0][j] - '0';if(maxLevel < level(0, j))maxLevel = level(0, j);}for(i = 0; i < matrixRowSize; i++){level(i, 0) = matrix[i][0] - '0';if(maxLevel < level(i, 0))maxLevel = level(i, 0);}/* 遍历矩阵 */for(i = 1; i < matrixRowSize; i++){for(j = 1; j < matrixColSize; j++){if(matrix[i][j] == '0')level(i, j) = 0;elselevel(i, j) = min(min(level(i-1, j), level(i, j-1)), level(i-1, j-1)) + 1;if(maxLevel < level(i, j))maxLevel = level(i, j);}}/* for debugfor(i = 0; i < matrixRowSize; i++){for(j = 0; j < matrixColSize; j++){printf("%c ", matrix[i][j]);}printf("\n");}printf("\n");for(i = 0; i < matrixRowSize; i++){for(j = 0; j < matrixColSize; j++){printf("%d ", level(i, j));}printf("\n");}printf("\n");
*/free(level);return (maxLevel * maxLevel);
}

[leetcode ]221. Maximal Square c语言相关推荐

  1. leetcode 221. Maximal Square | 221. 最大正方形(优化的暴力解法+动态规划解法)

    题目 https://leetcode.com/problems/maximal-square/ 题解 方法1:最暴力解 O((m*n)^2) public class Solution {publi ...

  2. LeetCode 221. Maximal Square (最大正方形)

    原题 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and ...

  3. LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium)

    welcome to my blog LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium) 题目描述 Given a ...

  4. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  5. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  6. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  7. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  8. 经典算法题之Maximal Square

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:叶    虎 Maximal Square是道非常有意思的算 ...

  9. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

最新文章

  1. Huawei FusionCompute安装体验
  2. 电力系统继电保护原理及仿真_电力系统继电保护(529页)
  3. Good Luck!_JAVA
  4. QuickSkin简单学习--控制结构
  5. Leetcode 1109.航班预定统计 差分
  6. 【BZOJ2754】【codevs2403】喵星球上的点名,AC自动机与STL的狂欢
  7. 严重漏洞可导致 Juniper 设备遭劫持或破坏
  8. 【慢慢学算法】:八进制(vector练习)
  9. SpringBoot启动自动执行sql脚本
  10. 21世纪的“影子王国”:GPT-3,又一场科技革命的来临
  11. 完美转换: Word表格转HTML
  12. 程序猿面试八股文分享~
  13. 阿里邮箱(@aliyun.com):启用IMAP功能+邮箱密码登录
  14. 任务栏WPS出现多窗口预览?下载这个注册表就对了
  15. YOLO train.txt创建
  16. Android 微光闪烁效果
  17. 数据库视图创建及应用
  18. 简单卷、跨区卷、带区卷、镜像卷和 RAID-5 卷
  19. 多网络情况下,Kafka客户端如何选择合适的网络发起请求
  20. 基于微信小程序的失物招领系统的设计与实现

热门文章

  1. 1.13_bucket_sort_桶排序
  2. js json数组_JaveScript对象篇和数组篇
  3. 使用 BoringSSL 优化 HTTPS 加密算法选择(不同终端加密算法不同)
  4. css按钮口诀 - CSS BUG顺口溜
  5. C#实现对文件目录的实时监控
  6. $watch, $watchCollection, $watchGroup的使用
  7. 不实例化图片,获取图片宽高的方法(vb.net)
  8. JSON.stringify( new WebSocket(ws://localhost:8080/websocket.do))
  9. Vue报错:3 errors and 0 warnings potentially fixable with the `--fix` option.
  10. ES6学习(六)—函数的扩展