Java for 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.
解题思路:
参考Java for LeetCode 130 Surrounded Regions BFS即可
JAVA实现如下:
public int numIslands(char[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[0].length; j++)
if (grid[i][j] == '1') {
grid[i][j] = '0';
bfs(grid, i * grid[0].length + j);
res++;
}
return res;
}
public static void bfs(char[][] grid, int num) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(num);
while (!queue.isEmpty()) {
num = queue.poll();
int row = num / grid[0].length;
int col = num - row * grid[0].length;
if (row - 1 >= 0 && grid[row - 1][col] == '1') {
grid[row - 1][col] = '0';
queue.add(num - grid[0].length);
}
if (row + 1 <= grid.length - 1 && grid[row + 1][col] == '1') {
grid[row + 1][col] = '0';
queue.add(num + grid[0].length);
}
if (col - 1 >= 0 && grid[row][col - 1] == '1') {
grid[row][col - 1] = '0';
queue.add(num - 1);
}
if (col + 1 <= grid[0].length - 1 && grid[row][col + 1] == '1') {
grid[row][col + 1] = '0';
queue.add(num + 1);
}
}
}
Java for LeetCode 200 Number of Islands的更多相关文章
- [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 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- [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 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 surro ...
- (BFS/DFS) 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 DFS
统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
随机推荐
- druid(德鲁伊)数据源的使用和配置 阿里出品
pom.xml <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...
- 【UVA 1586】Ancient Cipher
题 题意 给你一个只含CHON的有机物的化学式如C6H5OH求相对分子质量 分析 ... 代码 switch #include<cstdio> #include<cctype> ...
- 什么是谷歌loon计划
互联网服务已经与人类的生活密不可分,但受地理环境限制,目前全球只有三分之一的幸运儿能够体验到这种服务.为了让更多的人感受互联网,Google推出了一项名为“Project Loon”的计划,利用氢气球 ...
- 配置hibernate
http://blog.csdn.net/hanjiancanxue_liu/article/details/9966423
- 【codevs1257】 打砖块
http://codevs.cn/problem/1257/ (题目链接) 题意 在等腰三角形上打砖块,总共有m发炮弹,每块砖有一个权值,求打出的最大权值 Solution 今天考试题,考场上的2个小 ...
- BZOJ2301 莫比乌斯反演
题意:a<=x<=b,c<=y<=d,求满足gcd(x,y)=k的数对(x,y)的数量 ((x,y)和(y,x)不算同一个) 比hdu1695多加了个下界,还有 ...
- org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法
在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...
- mvc 简单笔记
---恢复内容开始--- 入口文件 index.php 唯一的一个让浏览器直接请求的脚本文件 控制器 协调模型和试图 模型 提供数据 保存数据 数据的验证 试图 只负责显示 <?php $c = ...
- xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi
xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") d ...
- 锋利的jQuery-4--动画方法总结简表