ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

200. Number of Islands

2019-05-29 17:50:18  阅读:270  来源: 互联网

标签:200 rows int Number cols queue grid Coordinate 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:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

Output: 3

这个题目可以使用bfs和dfs。

如果使用bfs,因为队列的时间复杂度太高了,可以使用stack代替。

class Coordinate {
        int x;
        int y;

        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    public int numIslands(char[][] grid) {
        int count = 0;
        int rows = grid.length;
        int cols = rows>0?grid[0].length:0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '0')
                    continue;
                Stack<Coordinate> queue = new Stack<>();
                count++;
                queue.add(new Coordinate(i, j));
                while (!queue.isEmpty()) {
                    Coordinate coordinate = queue.pop();
                    int x = coordinate.x;
                    int y = coordinate.y;
                    grid[x][y] = '0';
                    if (x + 1 < rows && grid[x + 1][y] != '0') {
                        queue.add(new Coordinate(x + 1, y));
                    }
                    if (y + 1 < cols && grid[x][y + 1] != '0') {
                        queue.add(new Coordinate(x, y + 1));
                    }
                    if (x - 1 >= 0 && grid[x - 1][y] != '0') {
                        queue.add(new Coordinate(x - 1, y));
                    }
                    if (y - 1 >= 0 && grid[x][y - 1] != '0') {
                        queue.add(new Coordinate(x, y - 1));
                    }


                }
            }
        }

        return count;


    }

 使用dfs递归完成后,是否需要恢复现场需要根据实际情况而定。

public class 岛屿的数量 {
	public int numIslands(char[][] grid) {
		if (grid == null || grid.length == 0 
				|| grid[0].length == 0)
			return 0;
		int rows = grid.length;
		int cols = grid[0].length;
		int count = 0;
		for (int i = 0; i < rows; i++)
			for (int j = 0; j < cols; j++)
				// 注意char
				if (grid[i][j] == '1') {
					count++;
					dfsSearch(grid, i, j, rows, cols);
				}
		return count++;
	}
 
	// 每遇到'1'后, 开始向四个方向 递归搜索. 搜到后变为'0',
	// 因为相邻的属于一个island. 然后开始继续找下一个'1'.
	private void dfsSearch(char[][] grid, 
			int i, int j, int rows, int cols) {
		if (i < 0 || i >= rows || j < 0 || j >= cols)
			return;
		if (grid[i][j] != '1')
			return;
		// 也可以才用一个visited数组,标记遍历过的岛屿
		grid[i][j] = '0';//不需要恢复现场
		dfsSearch(grid, i + 1, j, rows, cols);
		dfsSearch(grid, i - 1, j, rows, cols);
		dfsSearch(grid, i, j + 1, rows, cols);
		dfsSearch(grid, i, j - 1, rows, cols);
 
	}
}

  

标签:200,rows,int,Number,cols,queue,grid,Coordinate,Islands
来源: https://www.cnblogs.com/clarencezzh/p/10945257.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有