• 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:

    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);
     
    	}
    }
    

      

    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    Swift入坑系列—集合类型
    Java正则表达式入门
    Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
    Swift之手势总结
    Matlab图像彩色转灰色
    HDU1754_I Hate It(线段树/单点更新)
    Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02
    freemarker自己定义标签(二)
    hibernate 在tomcat7.X 下配置mysql数据源
    WinCE隐藏显示任务栏,当任务栏隐藏时将其显示,当任务栏显示时将其隐藏(FindWindow,ShowWindow,IsWindowVisible),
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/10945257.html
Copyright © 2020-2023  润新知