leetcode 221. Maximal Square 求一个数组中由1组成的最大的正方形面积 ---------- java...
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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.
给定一个二维数组,然后求里面由1组成的正方形的最大面积。
1、以每一个1作为左上角的顶点,然后想右边和下面扩展,看正方形有多大。
public class Solution {public int maximalSquare(char[][] matrix) {if (matrix.length == 0){return 0;}int row = matrix.length;int col = matrix[0].length;int result = 0;for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (matrix[i][j] == '1'){result = Math.max(result, getNum(matrix, i, j));}}}return result;}public int getNum(char[][] matrix, int row, int col){char ch = '1';int squre = 1;while (true){int num1 = row + squre;int num2 = col + squre;if (num1 >= matrix.length || num2 >= matrix[0].length){return squre * squre;}for (int i = row; i <= num1 ; i++){if (matrix[i][num2] != ch){return squre * squre;}}for (int i = col; i <= num2; i++){if (matrix[num1][i] != ch){return squre * squre;}}squre++;}}}
2、dp:遇到一个1,然后看[i - 1][j], [i - 1][j - 1],[i][j - 1]的值是多少,取最小值,然后加1。记录期间出现的最大值。
但是实际上没有第一种的速度快,因为有一个额外的row*col空间。
public class Solution {public int maximalSquare(char[][] matrix) {if (matrix.length == 0){return 0;}int result = 0;int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];for (int i = 0; i < matrix.length; i++){for (int j = 0; j < matrix[0].length; j++){if (matrix[i][j] == '1'){dp[i + 1][j + 1] = Math.min(Math.min(dp[i + 1][j], dp[i][j]), dp[i][j + 1]) + 1;result = Math.max(result, dp[i + 1][j + 1]);}}}return result * result;} }
转载于:https://www.cnblogs.com/xiaoba1203/p/6755972.html
leetcode 221. Maximal Square 求一个数组中由1组成的最大的正方形面积 ---------- java...相关推荐
- 算法12--topK求一个数组中第k大的数
求一个数组中第k大的值 解法一: 建立一个k个元素的最大堆,首先将数组中前k个元素放入堆中,此时堆顶元素为第k大的元素,后面继续遍历数组,比较堆顶元素与数组中元素值,当数组中元素小于堆顶元素时,将堆顶 ...
- C语言求一个数组中第k大的数,leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值...
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 初识递归,使用递归方法求一个数组中的最大值
找一个数组的最大值,方法很多,也很简单,一般再定义一个变量就可以了,就像下面这串代码. int []arr=new int [] {1,22,3,44,5,6,76,118,91};int max=a ...
- Java知识点串讲之简单的排序,求一个数组中的最大值
要求: 去计算一个整数数组中最大的整数元素, 思路: 我们可以假设在数组中存在一个最大值,然后用最大值依次去和其他数值进行比较,从而拿到真实中的最大值 接下来我们来看代码 import com.whh ...
- 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 ...
- [leetcode ]221. Maximal Square c语言
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- Leetcode-215:求一个数组中第k大的数
题目描述 给定一个未排序的整数数组,找到其中第k大的数 样例: 给出数组[4,5,1,2,3]和k=3,返回3: 给出数组[7,9,4,5]和k=1,返回9. 要求时间复杂度为O(n),空间复杂度为O ...
- leetcode 221. Maximal Square | 221. 最大正方形(优化的暴力解法+动态规划解法)
题目 https://leetcode.com/problems/maximal-square/ 题解 方法1:最暴力解 O((m*n)^2) public class Solution {publi ...
- 算法题:找出一个数组中相加值最大的连续序列元素
package arithmetic;/*** @author SHI* 求一个数组中相加值最大的连续序列元素*/ public class MaxSequence {public static vo ...
- 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 ...
最新文章
- SQL Server-聚焦使用视图若干限制/建议、视图查询性能问题,你懵逼了?(二十五)...
- VMware中安装CentOS Linux release 7.4.1708 (Core)
- HNCU 1741: 算法3-2:行编辑程序
- 关于CPU、指令集、架构、芯片概述
- 题解 [SHOI2014]概率充电器
- postman如何发送application/json类的post请求
- 【Python3网络爬虫开发实战】 2.3-爬虫的基本原理
- 通用汽车再次提高自动驾驶与电动汽车投资 增至350亿美元
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)-大壮他哥
- 22. 二叉树的层次遍历
- mysql表文件创建_php文件创建mysql的表
- 无线短距通信技术标准:WIFI,蓝牙,ZigBee
- DevExpress WPF初级教程 - 图像选择器的使用
- html 字体最小多少,浏览器最小显示12px字体的解决方法
- strip符号和节移除工具,符号表删除
- LVGL——lv_gauge 仪表盘
- html如何制作水滴效果图,html+css实现充电水滴融合特效代码
- css3属性基本骨架
- XSSFWorkbook下载excel表格
- 微信小程序video组件/腾讯视频插件,禁止拖动进度条,使用遮罩层的方式实现