Problem Surrounded Regions

Problem Description:

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

 Solution:

 1  public void solve(char[][] board) {
 2         Stack<Integer> sx  = new Stack<Integer>();
 3         Stack<Integer> sy = new Stack<Integer>();
 4
 5         if (board == null || board.length == 0) return;
 6         if (board[0].length == 0) return;
 7
 8         int row = board.length;
 9         int col  = board[0].length;
10
11         for (int i = 0; i < row; i++) {
12             if (board[i][0] == ‘O‘) {
13                 sx.push(i);
14                 sy.push(0);
15             }
16
17             if (board[i][col - 1] == ‘O‘) {
18                 sx.push(i);
19                 sy.push(col - 1);
20             }
21         }
22
23         for (int j = 0; j < col; j++) {
24             if (board[0][j] == ‘O‘) {
25                 sx.push(0);
26                 sy.push(j);
27             }
28
29             if (board[row - 1][j] == ‘O‘) {
30                 sx.push(row - 1);
31                 sy.push(j);
32             }
33         }
34         while (! sx.isEmpty()) {
35             int x = sx.pop();
36             int y = sy.pop();
37
38             board[x][y] = ‘#‘;
39
40             if (y > 0 && board[x][y - 1] == ‘O‘) {
41                 sx.push(x);
42                 sy.push(y - 1);
43             }
44
45             if (y < col - 1 && board[x][y + 1] == ‘O‘) {
46                 sx.push(x);
47                 sy.push(y + 1);
48             }
49
50             if (x > 0 && board[x - 1][y] == ‘O‘) {
51                 sx.push(x - 1);
52                 sy.push(y);
53             }
54             if (x < row - 1 && board[x + 1][y] == ‘O‘) {
55                 sx.push(x + 1);
56                 sy.push(y);
57             }
58
59         }
60         for (int i = 0; i < row; i++)
61             for (int j = 0; j < col; j++) {
62                 if (board[i][j] == ‘#‘) {
63                     board[i][j]= ‘O‘;
64                 } else {
65                     board[i][j] = ‘X‘;
66                 }
67             }
68
69     }

Problem Surrounded Regions

时间: 2024-11-26 00:54:59

Problem Surrounded Regions的相关文章

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

LeetCode: Surrounded Regions 解题报告

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 XX O O XX X O XX O X XAfter running your function, the

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  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 fu

[LintCode] 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.Example X X X XX O O XX X O XX O X X After capture all regions surrounded by 'X', the boar

[LeetCode][JavaScript]Surrounded Regions

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

【leetcode】Surrounded Regions

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

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

Surrounded Regions LeetCode :My Solution

Surrounded Regions Total Accepted: 14948 Total Submissions: 105121My Submissions 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 exampl

LeetCode: Surrounded Regions [130]

[题目] 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 sh