LeetCode 130. Surrounded Regions(包围区域)

11 篇文章 0 订阅
9 篇文章 0 订阅

原题网址:https://leetcode.com/problems/surrounded-regions/

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,

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

After running your function, the board should be:

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

方法:递归或者栈(但递归会溢出)。

public class Solution {
    private boolean[][] check;
    private boolean needCheck(char[][] board, boolean[][] checked, int i, int j) {
        if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false;
        if (checked[i][j] || board[i][j] == 'X') return false;
        return true;
    }
    private void check(char[][] board, boolean[][] checked, int i, int j) {
        if (!needCheck(board, checked, i, j)) return;
        Stack<Position> stack = new Stack<>();
        stack.push(new Position(i, j));
        while (!stack.isEmpty()) {
            Position position = stack.pop();
            if (!needCheck(board, checked, position.x, position.y)) continue;
            checked[position.x][position.y] = true;
            if (needCheck(board, checked, position.x-1, position.y)) stack.push(new Position(position.x-1, position.y));
            if (needCheck(board, checked, position.x+1, position.y)) stack.push(new Position(position.x+1, position.y));
            if (needCheck(board, checked, position.x, position.y-1)) stack.push(new Position(position.x, position.y-1));
            if (needCheck(board, checked, position.x, position.y+1)) stack.push(new Position(position.x, position.y+1));
        }
    }
    public void solve(char[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) return;
        boolean[][] checked = new boolean[board.length][board[0].length];
        for(int i=0; i<board.length; i++) {
            check(board, checked, i, 0);
            check(board, checked, i, board[0].length-1);
        }
        for(int j=0; j<board[0].length; j++) {
            check(board, checked, 0, j);
            check(board, checked, board.length-1, j);
        }
        for(int i=0; i<board.length; i++) {
            for(int j=0; j<board[i].length; j++) {
                if (checked[i][j]) board[i][j] = 'O';else board[i][j] = 'X';
            }
        }
    }
}
class Position {
    int x, y;
    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

另一种实现:

public class Solution {
    private int[] dy = {-1, 1, 0, 0};
    private int[] dx = {0, 0, -1, 1};
    private void find(char[][] board, int y, int x, boolean[][] visit) {
        visit[y][x] = true;
        LinkedList<Position> list = new LinkedList<>();
        list.add(new Position(y, x));
        while (!list.isEmpty()) {
            Position pos = list.remove();
            for(int i=0; i<4; i++) {
                int ny = pos.y + dy[i];
                int nx = pos.x + dx[i];
                if (ny < 0 || ny >= board.length || nx < 0 || nx >= board[ny].length) continue;
                if (visit[ny][nx] || board[ny][nx] == 'X') continue;
                visit[ny][nx] = true;
                list.add(new Position(ny, nx));
            }
        }
    }
    public void solve(char[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) return;
        boolean[][] visit = new boolean[board.length][board[0].length];
        for(int i=0; i<board.length; i++) {
            if (board[i][0] == 'O') find(board, i, 0, visit);
            if (board[i][board[i].length-1] == 'O') find(board, i, board[i].length-1, visit);
        }
        for(int i=0; i<board[0].length; i++) {
            if (board[0][i] == 'O') find(board, 0, i, visit);
            if (board[board.length-1][i] == 'O') find(board, board.length-1, i, visit);
        }
        
        for(int i=0; i<board.length; i++) {
            for(int j=0; j<board[i].length; j++) {
                if (board[i][j] == 'O' && !visit[i][j]) board[i][j] = 'X';
            }
        }
    }
}
class Position {
    int y, x;
    Position(int y, int x) {
        this.y = y;
        this.x = x;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值