【leetcode刷题笔记】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


题解:用的BFS。

最外围的O肯定是没法变成X的,把这些O推进一个队列里面,然后从它们开始上下左右进行广度优先搜索,它们周围的O也是不用变成X的(因为它有一条突围出去的都是O的路径),然后从周围的点继续广搜......

实现细节:

  • 用一个visited二维数组记录某个O是否已经进过队列了,这样就不会死循环;
  • 另一个二维数组isX记录哪些点不用变成X,在BFS结束后,把要变成X的点都变成X;
  • 题目中X和O都是大写的=。=

实现代码如下:

 1 public class Solution {
 2     class co{
 3         int x;
 4         int y;
 5         public co(int x,int y){
 6             this.x = x;
 7             this.y = y;
 8         }
 9     }
10     public void solve(char[][] board) {
11         if(board == null || board.length == 0)
12             return;
13         int m = board.length;
14         int n = board[0].length;
15         boolean[][] visited = new boolean[m][n];
16         boolean[][] isX = new boolean[m][n];
17         for(boolean[] row:isX)
18             Arrays.fill(row, true);
19         //put all o‘s cordinates into queue
20         Queue<co> queue = new LinkedList<co>();
21
22         //first line and last line
23         for(int i  = 0;i < n;i++)
24         {
25             if(board[0][i]==‘O‘){
26                 queue.add(new co(0, i));
27                 visited[0][i]= true;
28                 isX[0][i]= false;
29             }
30             if(board[m-1][i]==‘O‘){
31                 queue.add(new co(m-1, i));
32                 visited[m-1][i]= true;
33                 isX[m-1][i]= false;
34             }
35         }
36
37         //first and last column
38         for(int j = 0;j<m;j++){
39             if(board[j][0]==‘O‘){
40                 queue.add(new co(j, 0));
41                 visited[j][0]= true;
42                 isX[j][0]= false;
43             }
44             if(board[j][n-1]==‘O‘){
45                 queue.add(new co(j,n-1));
46                 visited[j][n-1]= true;
47                 isX[j][n-1]= false;
48             }
49         }
50
51         while(!queue.isEmpty()){
52             co c = queue.poll();
53             //up
54             if(c.x >= 1 && board[c.x-1][c.y] == ‘O‘&&!visited[c.x-1][c.y]){
55                 visited[c.x-1][c.y] = true;
56                 isX[c.x-1][c.y] = false;
57                 queue.add(new co(c.x-1, c.y));
58             }
59             //down
60             if(c.x+1<m && board[c.x+1][c.y]==‘O‘ && !visited[c.x+1][c.y]){
61                 visited[c.x+1][c.y] = true;
62                 isX[c.x+1][c.y]= false;
63                 queue.add(new co(c.x+1, c.y));
64             }
65             //left
66             if(c.y-1>=0 && board[c.x][c.y-1]==‘O‘ && !visited[c.x][c.y-1]){
67                 visited[c.x][c.y-1] = true;
68                 isX[c.x][c.y-1] = false;
69                 queue.add(new co(c.x, c.y-1));
70             }
71             //right
72             if(c.y+1<n && board[c.x][c.y+1] == ‘O‘ && !visited[c.x][c.y+1]){
73                 visited[c.x][c.y+1] = true;
74                 isX[c.x][c.y+1] = false;
75                 queue.add(new co(c.x, c.y+1));
76             }
77         }
78         for(int i = 0;i < m;i++){
79             for(int j = 0;j < n;j++){
80                 if(isX[i][j] )
81                     board[i][j]= ‘X‘;
82             }
83         }
84         return;
85     }
86 }
时间: 2024-10-22 14:18:17

【leetcode刷题笔记】Surrounded Regions的相关文章

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2