【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 surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
思路:题目提示是DFS,也就是递归解决问题,但是一时无法想到好的递归办法。后面参考了别人的代码,发现是需要做一层转换,当碰到'1'的时候,递归的把这个'1'及其周围的'1'都变成'0',这样就相当于消灭了一个island。于是乎有了下面的代码,其实还是很简单的哈,但是这个转换实在是没想到,以后要记住。
代码:
public class Solution {
public int numIslands(char[][] grid) {
int ret = 0;
for(int i=0;i<grid.length;i++) {
for(int j=0;j<grid[0].length;j++) {
if(grid[i][j] == '1') {
dfs(grid, i, j);
ret++;
}
}
}
return ret;
}
void dfs(char[][] grid, int i, int j) {
if(i<0 || i>= grid.length || j<0 || j>=grid[0].length || grid[i][j] != '1') return;
grid[i][j] = '0';
dfs(grid, i-1, j);
dfs(grid, i+1, j);
dfs(grid, i, j-1);
dfs(grid, i, j+1);
}
}
【leetcode】200. Number of Islands的更多相关文章
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】200. Number of Islands (2 solutions)
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【刷题-LeetCode】200 Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 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 s ...
- 【leetcode】1254. Number of Closed Islands
题目如下: Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally ...
随机推荐
- PHP扩展--Suhosin保护PHP应用系统
什么是Suhosin? Suhosin是一个PHP程序的保护系统.它的设计初衷是为了保护服务器和用户抵御PHP程序和PHP核心中,已知或者未知的缺陷. Suhosin有两个独立的部分,使用时可以分开使 ...
- hdu5909 Tree Cutting
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5909 [题解] 设$f_{x,i}$表示以$x$节点的子树中,权值为$i$的子树个数,其中$x$必选. ...
- NYOJ 1237 最大岛屿 (深搜)
题目链接 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋. ...
- ASP.NET 设置DropDownList的当前选项
1.通过显示字符Text DropDownList.Items.FindByText("你的值").Selected=true; 如果在设置之前,进行过设置,应用如下格式: Dro ...
- End to End Sequence Labeling via Bidirectional LSTM-CNNs-CRF论文小结
本篇论文是卡内基梅隆大学语言技术研究所2016年 arXiv:1603.01354v5 [cs.LG] 29 May 2016 今天先理解一下这个是什么意思: 找到的相关理解:arXi ...
- 排序中topK那点事(转)
问题描述:有 N (N>1000000)个数,求出其中的前K个最小的数(又被称作topK问题). 这类问题似乎是备受面试官的青睐,相信面试过互联网公司的同学都会遇到这来问题.下面由浅入深,分析一 ...
- 【转】jpeg文件格式详解
JPEG(Joint Photographic Experts Group)是联合图像专家小组的英文缩写.它由国际电话与电报咨询委员会CCITT(The International Telegraph ...
- PHP序列化、反序列化常用的魔术方法
__wakeup() //使用unserialize时触发__sleep() //使用serialize时触发__destruct() //对象被销毁时触发__call() //在对象上下文中调用不可 ...
- exit() _exit() 函数区别
exit(): --stdlib.h (1) 所有使用atexit()注册的函数,将会被以注册相反的顺序调用: (2) 所有打开的输出流被刷新,并且关闭流: (3) 使用tmpfile()创建的文件将 ...
- Linux(Unix)密码策略问题导致root密码不能修改
Linux(Unix)密码策略问题导致root密码不能修改 发布时间: 2016-01-19 浏览次数: 1034 下载次数: 5 用户修改了密码配置文件,导致root账户修改密码时报如下错误: ...