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

Return 4.

思路:

  DP,到达当前(非0)结点的最长边长square[i][j] = min(square[i-1][j-1], min(square[i-1][j], square[i][j-1])) + 1,即取左上方3个结点中值最小的一个加1。代码写的看起来好不舒服 - -!

 class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
const int rows = matrix.size();
if(rows == )
return ; vector<vector<char> >::iterator it = matrix.begin();
const int columns = (*it).size(); int square[rows][columns], ret = ; for(int i = ; i < rows; i++)
for(int j = ; j < columns; j++)
square[i][j] = ; for(int i = ; i < rows; i++){
if(matrix[i][] == '')
{
square[i][] = ;
ret = ;
}
} for(int i = ; i < columns; i++){
if(matrix[][i] == '')
{
square[][i] = ;
ret = ;
}
} for(int i = ; i < rows; i++)
{
for(int j = ; j < columns; j++)
{
square[i][j] = matrix[i][j] == '' ? min(square[i-][j], min(square[i][j-], square[i-][j-])) + : ; if(square[i][j] > ret)
ret = square[i][j];
}
}
return ret * ret;
}
};

【LeetCode 221】Maximal Square的更多相关文章

  1. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  3. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  4. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  5. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  6. 【LeetCode题解】350_两个数组的交集Ⅱ

    目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...

  7. 【LeetCode题解】349_两个数组的交集

    目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...

  8. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  9. 【LeetCode题解】144_二叉树的前序遍历

    目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...

随机推荐

  1. 【POJ3243】拓展BSGS(附hash版)

    上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...

  2. Linux Versus Windows, Ubuntu/Mint V XP/Vista/7

    原文:http://petermoulding.com/linux_versus_windows_ubuntu_mint_v_xp_vista_7 Linux Versus Windows, Ubun ...

  3. DP:斐波纳契数

    题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...

  4. lintcode:搜索二维矩阵II

    题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...

  5. lintcode:Remove Element 删除元素

    题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响.  样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 ...

  6. 【nginx网站性能优化篇(3)】反向代理实现负载均衡

    注意,本篇文章为负载均衡的理论篇,后续找个机会推出实战篇.理论篇主要讲述如何配置负载均衡,配置负载均衡不难.难的是真正的实战,比如如何做到多服务器之间的数据共享(session,file等),多cac ...

  7. arcgis api for javascript 出现 undefinedModule错误

    一般都是script代码里面语法错误,如. .;:之类的

  8. Dictionary<Key,Value>的用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. windows系统下Python环境的搭建

    1.下载最新的Python版本3.5.0.

  10. 61. Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...