LeetCode 130. 被围绕的区域

目录结构

 

1.题目

2.题解

2.1 DFS

2.2 BFS


1.题目

给定一个二维的矩阵,包含 'X' 和 'O'字母 O)。

找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。

示例:

X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:

X X X X
X X X X
X X X X
X O X X

解释:

被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/surrounded-regions
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2.题解

根据题设【任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'】,因此可以先dfs或bfs扫描检测出与边界‘O’相连的所有字母‘O’,这部分是不会被‘X’填充的,而其余的‘O’全填充为‘X’。

2.1 DFS

public class Solution130 {

    @Test
    public void test130() {
        char[][] board = {
                {'X', 'O', 'X', 'X'},
                {'X', 'X', 'X', 'X'},
                {'X', 'O', 'O', 'X'},
                {'X', 'X', 'O', 'O'},
                {'X', 'O', 'O', 'X'}
        };
        solve(board);
        System.out.println(Arrays.deepToString(board));
    }

    int len1, len2;

    public void solve(char[][] board) {
        len1 = board.length;
        if (len1 == 0) {
            return;
        }
        len2 = board[0].length;
        for (int r = 0; r < len1; r++) {
            dfs(board, r, 0);
            dfs(board, r, len2 - 1);
        }
        for (int c = 1; c < len2 - 1; c++) {
            dfs(board, 0, c);
            dfs(board, len1 - 1, c);
        }
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }

    public void dfs(char[][] board, int r, int c) {
        if (r < 0 || r >= len1 || c < 0 || c >= len2 || board[r][c] != 'O') {
            return;
        }
        board[r][c] = 'A';
        dfs(board, r + 1, c);
        dfs(board, r - 1, c);
        dfs(board, r, c + 1);
        dfs(board, r, c - 1);
    }
}
  • 时间复杂度:O(mn)
  • 空间复杂度:O(mn)

2.2 BFS

public class Solution130 {

    @Test
    public void test130() {
        char[][] board = {
                {'X', 'O', 'X', 'X'},
                {'X', 'X', 'X', 'X'},
                {'X', 'O', 'O', 'X'},
                {'X', 'X', 'O', 'O'},
                {'X', 'O', 'O', 'X'}
        };
        solve(board);
        System.out.println(Arrays.deepToString(board));
    }

    public void solve(char[][] board) {
        int len1 = board.length;
        if (len1 == 0) {
            return;
        }
        int len2 = board[0].length;
        int[] dx = {0, 0, 1, -1};
        int[] dy = {1, -1, 0, 0};
        Queue<int[]> queue = new ArrayDeque<>();
        for (int r = 0; r < len1; r++) {
            if (board[r][0] == 'O') {
                queue.offer(new int[]{r, 0});
            }
            if (board[r][len2 - 1] == 'O') {
                queue.offer(new int[]{r, len2 - 1});
            }
        }
        for (int c = 1; c < len2 - 1; c++) {
            if (board[0][c] == 'O') {
                queue.offer(new int[]{0, c});
            }
            if (board[len1 - 1][c] == 'O') {
                queue.offer(new int[]{len1 - 1, c});
            }
        }

        while (!queue.isEmpty()) {
            int[] cell = queue.poll();
            int x = cell[0], y = cell[1];
            board[x][y] = 'A';
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                if (mx < 0 || my < 0 || mx >= len1 || my >= len2 || board[mx][my] != 'O') {
                    continue;
                }
                queue.offer(new int[]{mx, my});
            }
        }
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }
}
  • 时间复杂度:O(mn)
  • 空间复杂度:O(mn)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值