[LeetCode]130. Surrounded Regions

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

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


解法一:并查集

注意一种特殊情况会stackoverflow:

OOOOOOOOOO
XXXXXXXXXO
OOOOOOOOOO
OXXXXXXXXX
OOOOOOOOOO
XXXXXXXXXO
OOOOOOOOOO
OXXXXXXXXX
OOOOOOOOOO
XXXXXXXXXO

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值