zoukankan      html  css  js  c++  java
  • 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);
     
    	}
    }
    

      

    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    #C++初学记录(sort函数)
    #C++初学记录(贪心算法#结构体#贪心算法)
    #C++初学记录(初识汉诺塔)
    vuex中使用多模块时,如果不同模块中action有名字冲突该如何解决
    vue中二次封装别人组件,动态传属性使用v-bind="$attrs" 和 v-on="$listeners"
    在vue.config.js项目中配置proxy解决跨域问题
    Vue里报错:Maximum call stack size exceeded
    git报错:pre-commit hook failed (add --no-verify to bypass)
    十大排序算法,用JS写出来
    基数排序(JS代码)
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/10945257.html
Copyright © 2011-2022 走看看