Java 图的遍历-LeetCode200
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
大意就是给个图,找寻所有的子图。注意两点:1.该二维数组并不一定是方阵;2.数组有可能是空的情况
代码如下:
public class Solution {
class pos {
public int x;
public int y;
public pos(int x, int y) {
this.x = x;
this.y = y;
}
}
public int numIslands(char[][] grid) {
if (grid.length == 0)
return 0;
int count = 0;
int xl = grid.length, yl = grid[0].length;
int[][] visit = new int[xl][yl];
LinkedList<pos> lp = new LinkedList<Solution.pos>();
pos cur;
for (int i = 0; i < xl; i++) {
for (int j = 0; j < yl; j++) {
if (grid[i][j] == '0')
continue;
if (grid[i][j] == '1' && visit[i][j] == 1)
continue;
cur = new pos(i, j);
lp.push(cur);
while (lp.size() > 0) {
cur = lp.poll();
visit[cur.x][cur.y] = 1;
if (cur.x + 1 < xl && grid[cur.x + 1][cur.y] == '1'
&& visit[cur.x + 1][cur.y] != 1) {
lp.push(new pos(cur.x + 1, cur.y));
}
if (cur.x - 1 >= 0 && grid[cur.x - 1][cur.y] == '1'
&& visit[cur.x - 1][cur.y] != 1) {
lp.push(new pos(cur.x - 1, cur.y));
}
if (cur.y + 1 < yl && grid[cur.x][cur.y + 1] == '1'
&& visit[cur.x][cur.y + 1] != 1) {
lp.push(new pos(cur.x, cur.y + 1));
}
if (cur.y - 1 >= 0 && grid[cur.x][cur.y - 1] == '1'
&& visit[cur.x][cur.y - 1] != 1) {
lp.push(new pos(cur.x, cur.y - 1));
}
}
++count;
}
}
return count;
}
public static void main(String[] args) {
char[][] grid1 = { { '1', '1', '1', '1', '0' },
{ '1', '1', '0', '1', '0' }, { '1', '1', '0', '0', '0' },
{ '0', '0', '0', '0', '0' } }, grid2 = {
{ '1', '1', '0', '0', '0' }, { '1', '1', '0', '0', '0' },
{ '0', '0', '1', '0', '0' }, { '0', '0', '0', '1', '1' } }, grid3 = {
{ '1', '1', '1' }, { '0', '1', '0' }, { '1', '1', '1' } };
Solution s = new Solution();
System.out.println(s.numIslands(grid1));
System.out.println(s.numIslands(grid2));
System.out.println(s.numIslands(grid3));
}
}
我利用的是广度优先遍历,也可尝试深度优先遍历
Java 图的遍历-LeetCode200的更多相关文章
- Java实现 洛谷 P3916 图的遍历(反向DFS+记忆化搜索)
P3916 图的遍历 输入输出样例 输入 4 3 1 2 2 4 4 3 输出 4 4 3 4 import java.io.BufferedReader; import java.io.IOExce ...
- JAVA集合迭代遍历和特性介绍
数组.集合:都是一种容器,用一个对象管理多个对象:数组不能自动增长:只能存放同类型的元素 集合能自动扩容:部分集合允许存放不同类型的元素: 1.List: 有顺序的,允许存放重复的元素: 遍历:for ...
- 图的遍历——BFS
原创 裸一篇图的BFS遍历,直接来图: 简单介绍一下BFS遍历的过程: 以上图为例子,从0开始遍历,访问0,按大小顺序访问与0相邻的所有顶点,即先访问1,再访问2: 至此顶点0已经没有作用了,因为其本 ...
- 图的遍历——DFS
原创 图的遍历有DFS和BFS两种,现选用DFS遍历图. 存储图用邻接矩阵,图有v个顶点,e条边,邻接矩阵就是一个VxV的矩阵: 若顶点1和顶点5之间有连线,则矩阵元素[1,5]置1,若是无向图[5, ...
- 数据结构(三十二)图的遍历(DFS、BFS)
图的遍历和树的遍历类似.图的遍历是指从图中的某个顶点出发,对图中的所有顶点访问且仅访问一次的过程.通常有两种遍历次序方案:深度优先遍历和广度优先遍历. 一.深度优先遍历 深度优先遍历(Depth_Fi ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- 转!! Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 【转】Java中如何遍历Map对
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
随机推荐
- dp入门--poj 1163数塔
...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- SpringMVC 框架的搭建及基本功能的实现
首先新建一个WEB项目 导入jar包 我们基于Spring mvc框架进行开发,需要依赖一下的spring jar包: spring-aop-4.0.4.RELEASE.jar spring-bean ...
- 欢迎进入MyKTV前后台点歌系统展示
一个项目,一分收获:一个项目,一些资源.Ktv项目也是一样的,所以我想分享我的收获,让你们获得你需要的资源. 一. 那MyKTV点歌系统具体的功能有哪些呢?我们就来看看吧! 1.MyKTV前台功能: ...
- reconnectingwebsocket.js
// MIT License: // // Copyright (c) 2010-2012, Joe Walnes // // Permission is hereby granted, free o ...
- 手把手教android studio中安装Android Butterknife Zelezny (转)
原文地址:http://blog.csdn.net/xin917480852/article/details/51176524 用来快速生成findViewById() 安装方法: 打开Android ...
- js ---- 时间格式
Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- 使用JavaScript获取日期加随机数生成单号
今天学习Javascript,得到一个自动生成单号的JavaScript,留下日后备用: function getNowFormatDate() { var day = new Date(); var ...
- shell简单用法笔记(一)
一.linux中主要用的bash shell:查看linux系统中支持的shell种类可用: vim /etc/shell 执行shel脚步的方式: 1.赋予脚步可执行权限,使用相对或绝对路径调用该脚 ...
- UI控件(UISegmentedControl)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray* segmentArray = [[ ...