博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Number of Islands(200)
阅读量:6832 次
发布时间:2019-06-26

本文共 5195 字,大约阅读时间需要 17 分钟。

 总而言之

BFS的方法对于静态的图的联通块问题比较好解

Union Find 解决动态的图的联通块的问题

1. BFS 

TIME: O(MxN) 

Space: O(min(M,N)) because in worst case where the grid is filled with lands, the size of the queue can grow up to min(M,N)

class Point {    int x;    int y;    public Point(int x, int y){        this.x = x;        this.y = y;    }}   class Solution {    public int numIslands(char[][] grid) {        if(grid.length == 0) return 0;        int n = grid.length;        int m = grid[0].length;            //bfs        int island = 0;        int[] dx = new int[]{0,0,1,-1};        int[] dy = new int[]{1,-1,0,0};                for(int i = 0; i< n; ++i){            for(int j = 0 ; j< m; ++j){                if(grid[i][j] == '1'){                    island ++;                    grid[i][j] = '0';                    Queue
queue = new LinkedList<>(); queue.offer(new Point(i, j)); while(!queue.isEmpty()){ Point current = queue.poll(); for(int index = 0 ; index < 4 ; ++index){ int nextX = dx[index] + current.x; int nextY = dy[index] + current.y; if(valid(nextX,nextY, grid)){ queue.offer(new Point(nextX,nextY)); grid[nextX][nextY] = '0'; } } } } } } return island; } private boolean valid(int x, int y, char[][] grid){ if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length){ return false; } return (grid[x][y] == '1'); }}

2. DFS 

TIME: O(MxN) 

Space: O(M+N)  worst case that the grid map is filled with lands where DFS goes by M \times NM×N deep

public int numIslands(char[][] grid) {        int n = grid.length;        if(n == 0) return 0;        int m = grid[0].length;        int island = 0;        for(int i = 0; i < n; ++i){            for(int j = 0 ; j < m; ++j){                if(grid[i][j] == '1'){                    island ++;                    grid[i][j] = '0';                    dfs(grid, i, j);                }            }        }        return island;    }        private void dfs(char[][] grid, int x, int y){        int[] dx = new int[]{0,0,1,-1};        int[] dy = new int[]{1,-1,0,0};        for(int i = 0; i < 4; ++i){            int nextX = dx[i] + x;            int nextY = dy[i] + y;            if(valid(nextX, nextY, grid)){                grid[nextX][nextY] = '0';                dfs(grid, nextX, nextY);            }        }    }    private boolean valid(int x, int y, char[][] grid){        if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length){            return false;        }        return (grid[x][y] == '1');    }

 

3. Union Find

Time: O(MxN)

Space: O(M×N) as required by UnionFind data structure

 

首先是Union Find 的定义

Point 要override hashcode和equals两个 ** 用Objects自带的方法比较好

class Point{    int x;    int y;    public Point(int x, int y){        this.x = x;        this.y = y;    }    public int hashCode(){        return Objects.hash(this.x, this.y);    }    public boolean equals(Object p){        Point other = (Point)p;        return Objects.equals(other.x, this.x) && Objects.equals(other.y, this.y);            }}class UnionFind{    Map
map; int size; public UnionFind(char[][] grid){ map = new HashMap<>(); size = 0; for(int i = 0 ; i < grid.length; i++){ for(int j = 0; j < grid[0].length; j++){ if(grid[i][j] == '1'){ map.put(new Point(i,j),new Point(i,j)); size ++; } } } } public void union(Point p1, Point p2){ size--; map.put(findRoot(p1), findRoot(p2)); } public Point findRoot(Point p){ if(map.get(p) != p){ map.put(p, findRoot(map.get(p))); } return map.get(p); } public boolean sameUnion(Point p1, Point p2){ return findRoot(p1) == findRoot(p2); }}
class Solution {    public int numIslands(char[][] grid) {        UnionFind uf = new UnionFind(grid);        for(int i = 0; i < grid.length; ++i){            for(int j = 0 ; j < grid[0].length; ++j){                if(grid[i][j] == '1'){                    dfs(grid, i, j, uf);                }            }        }        return uf.size;    }        private void dfs(char[][] grid, int x, int y,UnionFind uf){        int[] dx = new int[]{0,0,1,-1};        int[] dy = new int[]{1,-1,0,0};        for(int i = 0; i < 4; ++i){            int nextX = dx[i] + x;            int nextY = dy[i] + y;            if(valid(nextX, nextY, grid) && !uf.sameUnion(new Point(nextX,nextY), new Point(x,y))){                uf.union(new Point(nextX,nextY), new Point(x,y));                dfs(grid, nextX, nextY, uf);            }        }    }    private boolean valid(int x, int y, char[][] grid){        if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length){            return false;        }        return (grid[x][y] == '1');    }}

 

转载于:https://www.cnblogs.com/HUTONG749/p/9565249.html

你可能感兴趣的文章
XML和XMLSocket(一) -- XML的基础知识
查看>>
[强烈推荐]ORACLE SQL:经典查询练手第四篇(不懂装懂,永世饭桶!)
查看>>
Struts知识问答
查看>>
MongoDB实战(4)MapReduce
查看>>
FTP自动化上传的Shell脚本
查看>>
【C++11 并发编程教程 - Part 3 : 锁的进阶与条件变量(bill译)】
查看>>
换Vista啦
查看>>
jsp 教程(一)
查看>>
【移动开发】Android中异步加载数据(二)AsyncTask异步更新界面
查看>>
tomcat 访问日志源码分析与应用
查看>>
AIX 系统故障之--扩展文件系统故障
查看>>
组织单位的管理权委派[为企业维护windows server 2008系列十二]
查看>>
github生成静态博客
查看>>
AngularJs $interval 和 $timeout
查看>>
HashMap源码解析
查看>>
centos7安装出现license information(license not accepted)解决办法
查看>>
java中泛型之类型通配符(?)
查看>>
NA-NP-IE系列实验55: 多区域OSPF 基本配置
查看>>
在Puppet中用ERB模板来自动配置Apache虚拟主机
查看>>
写给MongoDB开发者的50条建议Tip20
查看>>