zoukankan      html  css  js  c++  java
  • LeetCode | Surrounded Regions

    题目地址: https://leetcode.com/problems/surrounded-regions/#/description

    Given a 2D board containing 'X' and 'O' (the letter 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

    题目意思是:将没有被X包围的O字符全部修改为X

    解题思路:从矩阵的四个边查找字符为’O'的结点,然后从该节点出发使用广度优先搜素,将找到的节点改为'F'

    这题有几个细节问题需要注意,否则无法通过leetCode,已经在代码注释中体现了,这里直接上代码
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 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
     * <p>
     * After running your function, the board should be:
     * X X X X
     * X X X X
     * X X X X
     * X O X X
     *
     * 思路:从矩阵的四个边查找字符为’O'的结点
     * 然后从该节点出发使用广度优先搜素,将找到的节点改为'F'
     */
    public class LeetCode_21 {
    
        private static final int[][] offset = new int[][]{{-1, 0},{0, -1},{1, 0},{0, 1}};
        private static final int FREE = 'F';
        private static final int TRAVEL = 'T';
        public void solve(char[][] board) {
            if(board == null || board.length <= 1 || board[0].length <= 1){
                return;
            }
            int row = board.length;
            int col = board[0].length;
            //第一行和最后一行
            for (int i = 0; i < col; ++i) {
                if(board[0][i] == 'O'){
                    bfs(board, new Point(0, i));
                }
                if(board[row-1][i] == 'O'){
                    bfs(board, new Point(row-1, i));
                }
            }
            //第一列和最后一列
            for (int i = 1; i < row-1; ++i) {
                if(board[i][0] == 'O'){
                    bfs(board, new Point(i, 0));
                }
                if(board[i][col-1] == 'O'){
                    bfs(board,  new Point(i, col-1));
                }
            }
    
            for(int i = 0; i < board.length; ++i) {
                for (int j = 0; j < board[0].length; ++j) {
                    if(board[i][j] == TRAVEL) {
                        board[i][j] = 'O';
                    }else if(board[i][j] == 'O'){
                        board[i][j] = 'X';
                    }
                }
            }
        }
    
        /**
         * 采用广度优先搜素
         * @param board
         * @param point
         */
        public void bfs(char[][] board, Point point) {
            LinkedList<Point> queue = new LinkedList<>();
            queue.offer(point);
            while (!queue.isEmpty()) {
                Point node = queue.poll();
                board[node.x][node.y] = TRAVEL; //一定要修改,不然会重复添加
                for (Point point1 : expand(board, node)) {
                    queue.offer(point1);
                }
            }
        }
    
        /**
         * 找到相邻结点为‘O'的结点
         * @param board
         * @param node
         * @return
         */
        public List<Point> expand(char[][] board, Point node) {
            List<Point> list = new ArrayList<>();
            for(int i = 0; i < offset.length; ++i) {
                int offsetX = node.x + offset[i][0];
                int offsetY = node.y + offset[i][1];
    
                if (offsetX >= 0 && offsetX < board.length &&
                        offsetY>=0 && offsetY <board[0].length && board[offsetX][offsetY] == 'O') {
                    board[offsetX][offsetY] = FREE;
                    list.add(new Point(offsetX, offsetY));
                }
            }
            return list;
        }
    
        class Point {
            int x;
            int y;
            Point(int a, int b) {
                x = a;
                y = b;
            }
        }
    }


  • 相关阅读:
    七牛云上传图片
    找到当前字符串中最后一个/并获取之后的字符串
    jquery正则表达式验证:验证身份证号码
    apply()与call()的区别
    js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
    改变父元素的透明度,不影响子元素的透明度—css
    c实现生产者消费者问题。 windows下。
    python基础练习 dict切片
    html+css test1
    codewars[7]-python Friend or Foe?
  • 原文地址:https://www.cnblogs.com/huangyichun/p/6970467.html
Copyright © 2011-2022 走看看