Surrounded Regions

/*
 本算法答案由上岸科技提供。
 上岸科技是一个专致力于高效培养北美留学生拥有实际面试能力的团体。
 我们采用小班化线上,线下教学让学生更快,更好的学习到想要的知识。
 团队主要由一群怀揣情怀于美国高校毕业的一线IT公司工程师构成。
 我们坚信对于求职者算法并不是全部,合理的技巧加上适当的算法培训能够大大的提升求职成功概率也能大大减少刷题的痛苦。
 正如我们的信仰:我们教的是如何上岸而不仅是算法。
 更多信息请关注官网:https://www.shanganonline.com/
*/
class Solution {
    private static int[] xBias = {0, 0, 1, -1};
    private static int[] yBias = {1, -1, 0, 0};
    
    public void solve(char[][] board) {
        if (board == null || board.length == 0) {
            return;
        }
        int row = board.length;
        int col = board[0].length;
        Queue<Integer> queue = new ArrayDeque<>();
        
        for (int i = 0; i < board.length; i++) {
            if (board[i][0] == 'O') {
                board[i][0] = '-';
                queue.offer(i * col);
            }
            if (board[i][col - 1] == 'O') {
                board[i][col - 1] = '-';
                queue.offer(i * col + col - 1);
            }

        }
        for (int i = 0; i < board[0].length; i++) {
            if (board[0][i] == 'O') {
                board[0][i] = '-';
                queue.offer(i);
            }
            if (board[row - 1][i] == 'O') {
                board[row - 1][i] = '-';
                queue.offer((row - 1) * col + i);
            }
        }
        
        while (!queue.isEmpty()) {
            int num = queue.poll();
            int i = num / col;
            int j = num % col;
            for (int k = 0; k < xBias.length; k++) {
                int newI = i + xBias[k];
                int newJ = j + yBias[k];
                if (newI < 0 || newI >= row || newJ < 0 || newJ >= col || board[newI][newJ] != 'O') {
                    continue;
                }
                queue.offer(newI * col + newJ);
                board[newI][newJ] = '-';
            }
        }
        
        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] == '-') {
                    board[i][j] = 'O';
                }
            }
        }
    }
}

Last updated

Was this helpful?