LeetCode Number of Islands

原创 2015年07月09日 00:24:30

Description:

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

Solution:

很明显的BFS或者DFS,用flood fill搞定。

这里需要注意的是,新fill的点,在第一次遍历到的时候就改变颜色,grid[da][db] = '0';而不要再for k循环的外层改变,grid[a][b] = '0'; 按照前一种做法,能够避免很多重复的点入队,否则按照第二种会TLE。

import java.util.*;

public class Solution {
	int m, n;
	int dir[][] = new int[][] { { 1, 0 }, { 0, 1 }, { 0, -1 }, { -1, 0 } };

	public int numIslands(char[][] grid) {
		this.m = grid.length;
		if (m == 0)
			return 0;
		this.n = grid[0].length;

		LinkedList<Integer> mList = new LinkedList<Integer>();
		LinkedList<Integer> nList = new LinkedList<Integer>();

		int a, b, da, db;

		int count = 0;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++) {
				if (grid[i][j] != '1')
					continue;
				mList.add(i);
				nList.add(j);
				grid[i][j] = '0';
				count++;
				while (!mList.isEmpty()) {
					a = mList.poll();
					b = nList.poll();

					for (int k = 0; k < 4; k++) {
						da = a + dir[k][0];
						db = b + dir[k][1];
						if (valid(grid, da, db)) {
							mList.add(da);
							nList.add(db);
							grid[da][db] = '0';
						}
					}
				}
			}

		return count;
	}

	boolean valid(char[][] grid, int a, int b) {
		if (a < 0 || a >= m)
			return false;
		if (b < 0 || b >= n)
			return false;
		if (grid[a][b] == '1')
			return true;
		return false;
	}

	public static void main(String[] args) {
		Solution s = new Solution();
		System.out.println(5 & 6 & 7);
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 200. Number of Islands (并查集)

LeetCode 200. Number of Islands (并查集)LeetCode 200 Number of Islands 并查集 问题描述 解题思路 参考代码Tags: * Depth...

第三周:( LeetCode200 ) Number of Islands(c++)

原题:本题是经典的简单图论问题,求岛的个数也即求图上的连通区间的个数。可采用深搜或者广搜。假设共有n个点,时间复杂度为o(n)。本题笔者采用深搜的递归和非递归两种解法。将遍历过的“1”被改为“0”,其...

leetcode解题之200. Number of Islands Java版(岛屿的数量)

leetcode解题之200. Number of Islands Java版(岛屿的数量) ,深度优先遍历

<LeetCode OJ> 200 / 130 Number of Islands / Surrounded Regions

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium Given a 2d grid map of '1's ...

LeetCode 200. Number of Islands

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 sur...

leetcode Add to List 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 surr...

【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】

【200-Number of Islands(岛的数目)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao...

Leetcode-200. Number of Islands

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...

leetcode_Number of Islands_medium--dfs

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is sur...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)