【LeetCode从零单排】No221.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.
解法很巧妙,我也是看了discuss,首先列出最左一列和最上面一列,剩下的解法可以看这个
Basic idea is to iterate over all columns and rows of a matrix (starting with i=j=1). If value in a cell>0 and cells to the north, west, and north-west are >0, pick smallest value of those 3 cells, take it's square root, add 1, and assign square of new value to current cell. For example given matrix
1 1 1 1 1 1
1 1 1 0 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 0 1We get:1 1 1 1 1 1
1 4 4 0 1 4
1 4 9 1 1 4
1 4 9 4 4 4
1 4 9 9 9 9
1 4 9 16 0 1
Our answer is the largest value in new matrix: 16
代码
public class Solution {public int maximalSquare(char[][] matrix) {if(matrix == null || matrix.length == 0) {return 0;}int res = 0;int m = matrix.length;int n = matrix[0].length;int[][] sq = new int[m][n];for(int i = 0; i < m; i++) {if(matrix[i][0] == '1') {sq[i][0] = 1;res = 1;} else {sq[i][0] = 0;}}for(int j = 0; j < n; j++) {if(matrix[0][j] == '1') {sq[0][j] = 1;res = 1;} else {sq[0][j] = 0;}}for(int i = 1; i < m; i++) {for(int j = 1; j < n; j++) {if(matrix[i][j] == '1') {int min = Math.min(Math.min(sq[i][j-1], sq[i-1][j]), sq[i-1][j-1]);//if(min != 0) {sq[i][j] = (int)Math.pow(Math.sqrt(min)+1, 2);res = Math.max(res, sq[i][j]);//}}}}return res;}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No221.Maximal Square相关推荐
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- 【LeetCode从零单排】No96 Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex ...
最新文章
- NB-IOT UE的小区接入过程
- 顶会论文9篇,又斩获百度奖学金!哈工大NLP“新生代”正崭露头角
- linux 用户态 隐藏进程 简介
- OpenCASCADE绘制测试线束:数据交换命令之XDE 形状命令
- Build Roads
- 【kafka】scalac compiled against an incompatible version of joptsimple
- 尚硅谷 硅谷新闻_硅谷如何将真正的创新变成一种夸大的幻想
- 通俗易懂的Spatial Transformer Networks(STN)(一)
- [每日一氵] Python内部创建一个进程,等用完再结束掉他
- basic auth java_java发送带Basic Auth使用 httpclient自带的认证方式
- 某蒟蒻无聊竟用UNO做了个复读机?
- 日记app制作(OC)
- IAR下载程序只有提示音,没有任何反应。或报错The configuration does not have debuggable output.(A debug-only project shoul
- 如何搭建职业教育智慧课堂?
- 【linux】md5sum 命令详解
- 【TMI2023】Contrastive Semi-Supervised Learning for Domain Adaptive Segmentation Across Similar Anatom
- 过滤器、拦截器、AOP、ControllerAdvcie的使用对比、执行顺序及代码教程
- Windows不能在本地计算机启动TOMCAT
- 语音控制开关灯----mega2560
- 叶面积指数(LAI)介绍以及遥感估算方法
热门文章
- messenger支持查找附近的人功能吗_最新的 macOS Catalina 正式版,值得更新吗?
- 主页被挟持 火狐浏览器_看过来!关于IE、360浏览器访问学校部分网站的设置说明在这里...
- idea 升级到2020后 无法启动_i.MXRT软复位后无法从32MB Flash启动?
- 噬血代码进不了游戏_玩家认可,二次元魂类游戏,《噬血代码》在三个方面做出了差异化...
- oracle swap忽略可以吗,Oracle Swap居高不下处理
- mysql批量修改http为https,墨涩网 - typecho系统升级全站https数据库批量替换网址/内容——墨涩网...
- 51单片机的中断系统
- oracle迁移postsql的,osdba's blog : Oracle迁移PostgreSQL系列文章之二:merge语句
- linux permit用法,技术|12 条实用的 zypper 命令范例
- 清空oracle表数据 外键,oracle清空所有表数据