【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 ...
随机推荐
- SVN服务器更换IP,客户端重新定位
svn服务器更换ip,后客户端需要重新定位,操作如下: 1.找到你的项目文件所在的根目录,右键点击空白地方,弹出菜单 TortoiseSVN-->Relocate 点击Relocate ,弹出重 ...
- 【BZOJ3453】XLkxc [拉格朗日插值法]
XLkxc Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定 k,a,n,d,p f(i ...
- node.js 开发命令行工具 发布npm包
新建一个文件夹“nodecmd”: 在nodecmd下新建文件夹bin: 在bin文件夹下新建JavaScript文件hello.js #!/usr/bin/env node console.log( ...
- 时间盲注脚本.py
时间盲注脚本 #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import time payloads = 'abcdefg ...
- linux编程之信号
信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...
- Redis 分片实现 Redis Shard [www]
Redis 分片实现 Redis Shard https://www.oschina.net/p/redis-s ...
- 自定义shell开头PS1
vim /etc/profile export PS1="flag:\W \u\$" \h是主机名,并不全,域 \W是当前所在目录名 \u 是当前shell用户名
- swiper 滑动插件,小屏单个显示滑动,大屏全部显示
var currSwiperIndex=0; function widthHandle(){ var level = widthLevel(); if(level==1){ //单个显示,滑动 if( ...
- tomcat - gc日志输出
原创 2017年01月04日 14:32:37 2090 tomcat/bin catalina.sh JAVA_OPTS='-server -Xms4g -Xmx4g -Xss256k -XX:Pe ...
- JMeter------ _time 函数的使用(时间戳、当前时间)
操作步骤: 1.通过函数助手,生成一个_time 函数: 2.如果参数为时间戳,那公式为: ${__time(,)} : 默认该公式精确到毫秒级别, 13位数 ${__time(/1000,)} ...