[LeetCode]Surrounded Regions

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


解法一:深搜

考虑一种特殊情况:

OOOOOOOOOO
XXXXXXXXXO
OOOOOOOOOO
OXXXXXXXXX
OOOOOOOOOO
XXXXXXXXXO
OOOOOOOOOO
OXXXXXXXXX
OOOOOOOOOO
XXXXXXXXXO

 应该change "if(j>=1)" to "if(j>1)",否则会stackoverflow。


public class Solution {
    int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    public void solve(char[][] board) {
        if (board == null || board.length == 0) return;
        for (int i = 0; i < board.length; i++) {
            bfs(board, i, 0);
            bfs(board, i, board[0].length - 1);
        }
        for (int i = 0; i < board[0].length; i++) {
            bfs(board, 0, i);
            bfs(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] == 'O') board[i][j] = 'X';
                else if (board[i][j] == '1') board[i][j] = 'O';
            }
        }
    }
    private void bfs(char[][] board, int i, int j) {
        if (board[i][j] != 'O' || board[i][j] == '1') return;
        board[i][j] = '1';
        for (int[] dir : dirs) {
            int row = dir[0] + i, col = dir[1] + j;
            if (row > 0 && row < board.length - 1 && col > 0 && col < board[0].length - 1 && board[row][col] == 'O') {
                bfs(board, row, col);
            }
        }
    }
}



解法二:union find
并查集两步走,一步union、一步find。初始状态将unionSet[i] = i,但是union之后只有根节点对应unionSet[i] = i,其他节点的unionSet[i]为父节点位置。

初始化的时候,把所有位于边处且对应值为‘O’的hasEdge初始化为true

二维数组展开成一维,遍历的时候x = i / width, y = i % width


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++) {
            // up\down里面二取一,right\left里面二取一即可,不一定是哪一组
            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、付费专栏及课程。

余额充值