[LeetCode]130. Surrounded Regions

来源:互联网 发布:sql 修改 primary key 编辑:程序博客网 时间:2024/05/22 13:46

https://leetcode.com/problems/surrounded-regions/

目前做到过的union find的题目都是两解,并查集或者bfs+递归。


解法一:并查集

注意一种特殊情况会stackoverflow:

OOOOOOOOOOXXXXXXXXXOOOOOOOOOOOOXXXXXXXXXOOOOOOOOOOXXXXXXXXXOOOOOOOOOOOOXXXXXXXXXOOOOOOOOOOXXXXXXXXXO

public class Solution {    public void solve(char[][] board) {        if (board == null || board.length == 0 || board[0].length == 0) {            return;        }        for (int i = 0; i < board.length; i++) {            dfs(board, i, 0);            dfs(board, i, board[0].length - 1);        }        for (int i = 0; i < board[0].length; i++) {            dfs(board, 0, i);            dfs(board, board.length - 1, i);        }        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[0].length; j++) {                if (board[i][j] == '1') {                    board[i][j] = 'O';                } else if (board[i][j] == 'O') {                    board[i][j] = 'X';                }            }        }    }    int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};    private void dfs(char[][] board, int x, int y) {        if (board[x][y] == 'O') {            board[x][y] = '1';            for (int[] dir : dirs) {                int row = dir[0] + x;                int col = dir[1] + y;                // 为避免stackoverflow,因此row < board.length - 1, col < board[0].length - 1                if (row >= 0 && row < board.length - 1 && col >= 0 && col < board[0].length - 1 && board[row][col] == 'O') {                    dfs(board, row, col);                }            }        }    }}



解法二:并查集,保存两个数组id和是否连通最外边。并查集总是把二维转化成一维数组。顺序便利的时候union当前位置左侧和上方的相同元素的位置。最后遍历一遍,如果当前位置的root不与最外边联通,那就置为X,否则不动。

public class Solution {    boolean[] hasEdge;    int[] unionSet;    public void solve(char[][] board) {        if (board == null || board.length == 0 || board[0].length == 0) return;        int height = board.length, width = board[0].length;        hasEdge = new boolean[height * width];        unionSet = new int[hasEdge.length];        for (int i = 0; i < unionSet.length; i++) {            unionSet[i] = i;        }        for (int i = 0; i < unionSet.length; i++) {            int x = i / width, y = i % width;            hasEdge[i] = (board[x][y] == 'O' && (x == 0 || x == height - 1 || y == 0 || y == width - 1));        }        for (int i = 0; i < unionSet.length; i++) {            int x = i / width, y = i % width, up = x - 1, left = y - 1;            if (up >= 0 && board[up][y] == board[x][y]) union(i, i - width);            if (left >= 0 && board[x][left] == board[x][y]) union(i, i - 1);        }        for (int i = 0; i < unionSet.length; i++) {            int x = i / width, y = i % width;            if (board[x][y] == 'O' && !hasEdge[findSet(i)]) {                board[x][y] = 'X';            }        }    }    private void union(int i, int j) {        int rootX = findSet(i);        int rootY = findSet(j);        boolean temp = hasEdge[rootX] || hasEdge[rootY];        unionSet[rootX] = rootY;        hasEdge[rootY] = temp;    }    private int findSet(int i) {        if (unionSet[i] == i) return i;        unionSet[i] = findSet(unionSet[i]);        return unionSet[i];    }}


0 0
原创粉丝点击