博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode#搜索#水域大小模型总结#19.水域大小
阅读量:3952 次
发布时间:2019-05-24

本文共 1144 字,大约阅读时间需要 3 分钟。

Difficulty: 中等

你有一个用于表示一片土地的整数矩阵land,该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接的水域的个数。编写一个方法来计算矩阵中所有池塘的大小,返回值需要从小到大排序。

示例:

输入:[  [0,2,1,0],  [0,1,0,1],  [1,1,0,1],  [0,1,0,1]]输出: [1,2,4]

提示:

  • 0 < len(land) <= 1000
  • 0 < len(land[i]) <= 1000

Solution

Language: 全部题目

class Solution {
public int[] pondSizes(int[][] land) {
List
list = new ArrayList<>(); int m = land.length, n = land[0].length; for(int i=0;i
0) list.add(area); } } list.sort((i,j)->i-j); return list.stream().mapToInt(Integer::intValue).toArray(); } int dfs(int x,int y,int m,int n,int[][]land) {
if(x<0 || x>=m) return 0; if (y<0 || y>=n) return 0; if(land[x][y]!=0) return 0; int area = 1; land[x][y] = 999; area += dfs(x+1,y,m,n,land); area += dfs(x-1,y,m,n,land); area += dfs(x,y+1,m,n,land); area += dfs(x,y-1,m,n,land); area += dfs(x+1,y+1,m,n,land); area += dfs(x-1,y-1,m,n,land); area += dfs(x+1,y-1,m,n,land); area += dfs(x-1,y+1,m,n,land); return area; } }

转载地址:http://juyzi.baihongyu.com/

你可能感兴趣的文章
Host 'ETCV3' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
查看>>
OCILIB在VS2008中的使用
查看>>
OCILIB VC2008 效率测试
查看>>
PL/SQL设置NUMBER显示为字符串
查看>>
linux ftp 脚本 -- 使用程序执行脚本
查看>>
MFC CListBox的使用
查看>>
VS2008向MFC 对话框 添加托盘图标(显示和消失)
查看>>
redhat中vsftp开机自启动
查看>>
MySQL存储过程,生成大量数据
查看>>
查询字段值出现多次的字段值
查看>>
SQL Server表存在则进行查重 SQL语句
查看>>
redhat 9 下sqlite 3的安装及编程
查看>>
两个同步表的字段复制.Oracle.
查看>>
windows MySQL 报“Got a packet bigger than 'max_allowed_packet' bytes”错误,解决过程.
查看>>
在Redhat9下静态编译glib库.
查看>>
CImg库编译使用.
查看>>
SQL Server循环执行动态SQL语句.
查看>>
ubuntu10.4网卡名由eth0改为eth4,导致获得不了IP地址.解决方法.
查看>>
CheckPoint关键词做字段名使用.
查看>>
Qt QSplitte分割器使用(用户手动改变窗口大小)
查看>>