算法:求岛屿的数量200. Number of Islands
200. Number of Islands
Input: grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]
]
Output: 1
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]
]
Output: 3
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] is '0' or '1'.
1. 递归解法
2层循环遍历是否为’1’, 如果是,则递归循环打掉所有上下左右的的’1’. 再接着遍历。如果还能优化,可以记录遍历过的记录,直接跳过打掉过的1.
class Solution {public int numIslands(char[][] grid) {int count = 0;for (int i = 0; i < grid.length; i++) {for (int k = 0; k < grid[0].length; k++) {if (grid[i][k] == '1') {count++;sink(grid, i, k);}}}return count;}private void sink(char[][] grid, int i, int k) {if (i < 0 || k < 0 || i >= grid.length || k >= grid[0].length || grid[i][k] == '0') return;grid[i][k] = '0';sink(grid, i+1, k);sink(grid, i, k+1);sink(grid, i-1, k);sink(grid, i, k-1);}
}
2. 递归求解,优化掉重复的sink函数
看到优雅的写法就是上下左右,用一个数组{0, 1, 0, -1, 0}
两两遍历,
- 下
0,1
- 右
1,0
- 上
0, -1
- 左
-1, 0
class Solution {public int numIslands(char[][] grid) {int count = 0;for (int i = 0; i < grid.length; i++) {for (int k = 0; k < grid[0].length; k++) {if (grid[i][k] == '1') {count++;sink(grid, i, k);}}}return count;}int[] d = {0, 1, 0, -1, 0};private void sink(char[][] grid, int i, int k) {if (i < 0 || k < 0 || i >= grid.length || k >= grid[0].length || grid[i][k] == '0') return;grid[i][k] = '0';for (int z = 0; z < 4; z++) {sink(grid, i + d[z], k + d[z + 1]);}}
}
算法:求岛屿的数量200. Number of Islands相关推荐
- 200. Number of Islands**(岛屿数量)
200. Number of Islands**(岛屿数量) https://leetcode.com/problems/number-of-islands/ 题目描述 Given an m x n ...
- 200.Number of Islands
/** 200.Number of Islands * 2016-4-3 by Mingyang* union 什么:两个相邻的1元素* union 目的:union后计数union集合数量(通过计数 ...
- LeetCode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode.200]Number of Islands
问题描述 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is ...
- leetcode 200. Number of Islands | 200. 岛屿数量(Java)
题目 https://leetcode-cn.com/problems/number-of-islands/ 题解 class Solution {public int numIslands(char ...
- LeetCode 200. Number of Islands--c++ dfs解法
LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Pyt ...
- 岛屿的个数java_LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- 【C++】设计算法求1000以内的质数数量
题目: 设计算法求1000以内(包含1000)的质数数量 //求1000以内的质数数量,分析算法的时间复杂度 //author:Mitchell_Donovan //date:2021.3.2 #in ...
- hdu 4160 Dolls 匈牙利算法求最大匹配
Dolls Time Limit: 2000 ...
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
最新文章
- 如何解决开发人员的工作无法量化的问题
- Android中解析XML---数据存储
- (五)boost库之随机数random
- AutoCAD.NET二次开发注意事项与开发技巧
- 数据库连接串MSSQL、Oracle、Access
- 关于Python3.7和Python3.6中元组类型数据内存存储问题
- 静态页面实现include
- 理解云原生--开发模型、架构原则、主要技术
- java将数据从外部读入到程序称为_java复习与练习答案beta3(基本可信~~)
- 3DMM-Fitting_Pytorch代码阅读
- 毕业设计——英文文献下载
- 小滴课堂-学习笔记:(1)JAVASE课程
- 这18个网站能让你的页面背景炫酷起来
- 28岁想入行软件测试,可行吗?
- win10开安卓模拟器蓝屏
- 坯子库曲面推拉教程_坯子助手下载_坯子助手最新版下载-下载之家
- 基于TCP,Socket编程,模仿腾讯QQ界面,使用Java开发的一款网络聊天工具。QQ_Chat
- 糖尿病遗传风险检测挑战赛
- ASP注入漏洞全接触[转自www.hacker.com.cn 黑客防线]
- 自定义三档半圆开关控件
热门文章
- python设计模式pdf_精通python设计模式豆瓣-精通python设计模式第二版电子书pdf下载-精品下载...
- 用计算机控制人造卫星和导弹发射,广西成人高校计算机实用基础统考理论试题(...
- SACC2018:教您如何实现大数据分析与精准推荐
- 面向开发者的最佳 Android 库列表
- socket发送http请求
- 在华为服务器 RH 2288H V2上装 windows 2008
- expect实现自动通过堡垒机登陆
- 数据结构 《18》----RMQ 与 LCA 的等价性 (一)
- 在演示文稿中控制视频播放效果
- IBM小型机维护手册