Leetcode176: Maximal Square

2024-08-28 19:32
文章标签 square maximal leetcode176

本文主要是介绍Leetcode176: Maximal Square,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.

用动态规划的思想,令res[i][j]表示以i,j为右下角坐标的正方形的最大边长,则 res[i][j] = min(min(res[i-1][j], res[i][j-1]), res[i-1][j-1])+1

class Solution {
public:int maximalSquare(vector<vector<char>>& matrix) {int m = matrix.size();if(m==0)return 0;int n = matrix[0].size();if(n==0)return 0;vector<vector<int>> res(m, vector<int>(n));int maxlen=0;for(int i = 0; i < m; i++){if(matrix[i][0] == '1'){res[i][0] = 1;maxlen = 1;}}for(int i = 0; i < n; i++){if(matrix[0][i] == '1'){res[0][i] = 1;maxlen = 1;}}for(int i = 1; i < m; i++){for(int j = 1; j < n; j++){if(matrix[i][j] == '1'){res[i][j] = min(min(res[i-1][j], res[i][j-1]), res[i-1][j-1])+1;maxlen = max(maxlen, res[i][j]);}else{res[i][j] = 0;}}}return maxlen*maxlen;}
};


这篇关于Leetcode176: Maximal Square的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115678

相关文章

[LeetCode] 221. Maximal Square

题:https://leetcode.com/problems/maximal-square/description/ #题目 Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input:

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Inpu

python 实现perfect square完全平方数算法

python 实现perfect square完全平方数算法介绍 完全平方数(Perfect Square)是一个整数,它可以表示为某个整数的平方。例如,1,4,9,16,25,… 都是完全平方数,因为 1 = 1 2 , 4 = 2 2 , 9 = 3 2 1=1^2,4=2^2,9=3^2 1=12,4=22,9=32,依此类推。 要判断一个给定的数 n 是否是完全平方数,有几种方法可以

Fill the Square

中文题目解释详见我的博客:http://xiaoshig.sinaapp.com/?p=94   In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty bl

Leetcode209: Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 给定一个矩阵中,只有0和1,求出这个矩阵的一个最大的子矩阵,其中只包含1. 例如 01101 11010 01110 11110 1

LeetCode | Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle 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

Ural 1073 Square Country (DP)

题目地址:Ural 1073 DP水题。也可以说是背包。 #include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#in

A. Theatre Square

A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard output Theatre Square in the capital city of Berland has

Codeforces #247 (Div. 2) A. Black Square

水题一道,4分钟AC 代码如下: #include <cstdio>#include <iostream>#include <algorithm>#define MAXN 100010#define ll long longusing namespace std;int a[MAXN];int main(void) {for(int i=1; i<=4; ++i) {cin >>

chi-square, chi-distribute与Guassian distribute近似

chi-distribute is closer to Guassian distribute than chi-square.