力扣算法之《130. 被围绕的区域》

[力扣算法题]《130. 被围绕的区域》

class Solution {
   	// Reappearance follow the leetcode official website solution of this question
    public void solve(char[][] board) {
        if(board == null || board.length == 0)return;
        int n = board.length;
        int m = board[0].length;
        for(int i = 0; i < n; ++i){
            dfs(board, i, 0);
            dfs(board, i, m - 1);
        }
        
        // for(int i = 1; i < m - 1; ++j){ // careless
        for(int i = 1; i < m - 1; ++i){
            dfs(board, 0, i);
            dfs(board, n - 1, i);
        }
        
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < m; ++j){
                if(board[i][j] == 'A'){
                    board[i][j] = 'O';
                }
                else if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                }
            }
        }
    }
    // I can't judge it's 'O' or '0' in this quesion.
    // 深度优先遍历
    public void dfs(char[][] board, int r, int c){
        int rl = board.length;
        int cl = board[0].length;
        // I can't judge it's 'O' or '0' in this quesion.
        if(r < 0 || r >= rl || c < 0 || c >= cl || 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);
    }
    // Reappearance follow the leetcode official website solution of this question
}

在这里插入图片描述

吐槽一下题目中的字符编码集,我看了半天都没区分是‘0’还是‘O’… 😦

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值