当前位置:网站首页>岛屿数量vs最大正方形
岛屿数量vs最大正方形
2022-07-21 12:15:00 【爪哇贡尘拾Miraitow】
岛屿数量
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
思路:
矩阵中多处聚集着1,要想统计1聚集的堆数而不重复统计,那我们可以考虑每次找到一堆相邻的1,就将其全部改成0,而将所有相邻的1改成0的步骤又可以使用深度优先搜索(dfs):当我们遇到矩阵的某个元素为1时,首先将其置为了0,然后查看与它相邻的上下左右四个方向,如果这四个方向任意相邻元素为1,则进入该元素,进入该元素之后我们发现又回到了刚刚的子问题,又是把这一片相邻区域的1全部置为0,因此可以用递归实现。
class Solution {
public int numIslands(char[][] grid) {
int m = grid.length;
if (m == 0)
return 0;
int n = grid[0].length;
int count = 0;
for(int i = 0; i<m;i++){
for(int j = 0;j<n;j++){
if(grid[i][j] == '1'){
count++;
dfs(grid,i,j);
}
}
}
return count;
}
public void dfs(char[][] grid, int i,int j){
int m = grid.length;
int n = grid[0].length;
grid[i][j]='0';
if(i-1>=0 && grid[i-1][j] == '1' )
dfs(grid,i-1,j);
if(i+1<m && grid[i+1][j] == '1' ){
dfs(grid,i+1,j);
}
if( j-1>=0 && grid[i][j-1] == '1'){
dfs(grid,i,j-1);
}
if( j+1<n && grid[i][j+1] == '1'){
dfs(grid,i,j+1);
}
}
}
最大正方形
在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。
class Solution {
public int maximalSquare(char[][] matrix) {
int maxSide = 0;
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return maxSide;
}
int rows = matrix.length, columns = matrix[0].length;
int[][] dp = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (matrix[i][j] == '1') {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
}
maxSide = Math.max(maxSide, dp[i][j]);
}
}
}
int maxSquare = maxSide * maxSide;
return maxSquare;
}
}
class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] dp = new int[m+1][n+1];
int max = 0 ;
for(int i = 1 ; i <= m ; i++ ){
for(int j = 1 ; j <= n ; j++){
if(matrix[i-1][j-1] == '1'){
dp[i][j] = 1+ Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
max = Math.max(max,dp[i][j]);
}
}
}
return max*max;
}
}
边栏推荐
- Records of relevant tools for flow analysis
- 可变与不可变数据类型
- 读Zepto源码之Touch模块
- Matlab r2014a help file cannot be copied
- . Replacewith() can only work once
- ionic4学习笔记11-某东项目热门商品展示
- 上海交大团队使用联合深度学习优化代谢组学研究
- When uploading jars on the nexus management page, jars can be pulled to the project normally. Jars published using the deploy of idea lifecycle can only be pulled to POM. 401 problem
- 4.基本类型和引用类型?
- 自动编码器的相关内容
猜你喜欢
如何恢复初次写博客时出现的-使用CSDN-markdown编辑器模板
Go语言 接口与类型
【BSP视频教程】BSP视频教程第20期:串口专题之玩转HAL库,LL库和寄存器方式实现方法以及参考手册几个关键时序图学习(2022-07-16)
Introduction to [MODBUS development] introductory teaching and protocol
Interview must ask: from entering URL to page display, what happened? (detailed and easy to understand, organized and easy to remember)
医学细胞图像分割
微信小程序
欢迎使用CSDN-markdown编辑器-初次使用编辑模板
Wuxi launched a major investigation of potential food safety hazards in Pizza Hut stores in the city
ICML2022最佳论文奖:从数百万个预测结构中学习蛋白质逆折叠
随机推荐
Admin组件
js如何控制整个页面滚动条的位置
JS how to control the position of the scroll bar of the whole page
交互式虚拟现实技术在药物发现中的新兴潜力
Medical cell image segmentation
Interviewer: please realize the modular implementation of map / filter / reduce | data base method
GAN网络的重新学习的一些内容记录
流量分析的相关工具记录
下载工具-谷歌插件 tampermonkey 和 greasyfork
IO多路复用
6.ES5新增的数组的方法?
es6 循环 过滤 取值
7.字符编码?
编码gbk的不可映射字符
Go语言 文件操作
第七节 数据字典:Hash哈希 跟着大宇学Redis--------目录帖
吴恩达撰文:如何建立适应AI职业生涯的项目
Welcome to CSDN markdown editor - first use of editing template
tabbar搭建
Go语言 接口与类型