1. 程式人生 > >LeetCode力扣之130. Surrounded Regions

LeetCode力扣之130. Surrounded Regions

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

package 
leetCode; import java.util.LinkedList; import java.util.Queue; /** * Created by lxw, [email protected] on 2018/3/26. */ class Point{ int x; int y; public Point(int x, int y){ this.x = x; this.y = y; } } public class L130_SurroundedRegions { public void solve(char[][] board){ if
(board.length == 0){ return; } int row = board.length; int col = board[0].length; Queue<Point> queue = new LinkedList<>(); for (int r = 0; r < row; r++){ if (board[r][0] == 'O'){ board[r][0] = '+'; queue.add(new Point(r, 0)); } if
(board[r][col -1] == 'O'){ board[r][col-1] = '+'; queue.add(new Point(r, col-1)); } } for (int c= 0; c < col; c++){ if (board[0][c] == 'O'){ board[0][c] = '+'; queue.add(new Point(0, c)); } if (board[row-1][c] == 'O'){ board[row-1][c] = '+'; queue.add(new Point(row-1, c)); } } while (!queue.isEmpty()){ Point p = queue.poll(); int r = p.x; int c = p.y; if (r-1 >= 0 && board[r-1][c] == 'O'){ board[r-1][c] = '+'; queue.add(new Point(r-1, c)); } if (r+1 < row && board[r+1][c] == 'O'){ board[r+1][c] ='+'; queue.add(new Point(r+1, c)); } if (c-1 >= 0 && board[r][c-1] == 'O'){ board[r][c-1] = '+'; queue.add(new Point(r, c-1)); } if (c+1 < col && board[r][c+1] == 'O'){ board[r][c+1] = '+'; queue.add(new Point(r,c+1)); } } for (int i = 0; i<row; i++){ for (int j=0; j<col; j++){ if (board[i][j] == 'O') board[i][j] = 'X'; if (board[i][j] == '+') board[i][j] = 'O'; } } } }

相關推薦

LeetCode130. Surrounded Regions

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 in

LeetCode135. Candy

There are N children standing in a line. Each child is assigned a rating value.You are giving candies

LeetCode-面試算法經典-Java實現】【130-Surrounded Regions(圍繞區域)】

pos apt pub iso all 左面 ons || title 【130-Surrounded Regions(圍繞區域)】 【LeetCode-面試算法經典-Java實現】【全部題目文件夾索引】 原題   Given a 2D b

(LeetCode 130)Surrounded Regions

Q: 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 surro

Leetcode 130. Surrounded Regions

class Solution{ public: void bfs(vector<vector<char>> &board, int r, int c) { board[r][c] = 'e'; if (r-1 >= 0 &

LeetCode-130. Surrounded Regions

0.原題 Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all

python leetcode 130. Surrounded Regions

注意遍歷的順序,從四周開始。與Pacific Atlantic Water Flow類似。 class Solution(object): def solve(self, board): """ :type board: List[List[str

Leetcode題解系列——130. Surrounded Regions(c++版)

題目大意:給出一個二維的圖,由X和O兩種字元構成。現在想要將符合條件的O變成X,條件為被X所包圍,且沒有連線的O處在四個邊沿上。 注意點: 注意判斷搜尋時,會出現越界情況 當傳入圖為空的處理情況 v

[LeetCode] 130. Surrounded Regions

題:https://leetcode.com/problems/surrounded-regions/description/ 題目 Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions

#leetcode#130. Surrounded Regions

iven a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is

LeetCode-面試演算法經典-Java實現】【130-Surrounded Regions(環繞區域)】

原題   Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.   A region is captured by flipping a

leetcode 130. Surrounded Regions 被圍繞的區域 c++

給定一個二維的矩陣,包含 'X' 和 'O'(字母 O)。 找到所有被 'X' 圍繞的區域,並將這些區域裡所有的 'O' 用 'X' 填充。 示例: X X X X X O O X X X O X X O X X 執行你的函式後,矩陣變為: X X X X X X

130. Surrounded Regions

ons void while boa apt pty 改變 int style Given a 2D board containing ‘X‘ and ‘O‘ (the letter O), capture all regions surrounded by ‘X‘.

130. Surrounded Regions 將包圍的符號變換 BFS & DFS & UNION find

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all '

【python/leetcode/130/M】Surrounded Regions

題目 https://leetcode.com/problems/surrounded-regions/ 基本思路 轉換一下思路,找出哪些O是沒有被X包圍的。在面板四周的O肯定是沒有被X包圍的,與它們相連的O也是沒有被包圍的,其它的O都是被X包圍的。 問題簡化為將與四周的

LeetCode】#130被圍繞的區域(Surrounded Regions)

【LeetCode】#130被圍繞的區域(Surrounded Regions) 題目描述 給定一個二維的矩陣,包含 ‘X’ 和 ‘O’(字母 O)。 找到所有被 ‘X’ 圍繞的區域,並將這些區域裡所有的 ‘O’ 用 ‘X’ 填充。 示例 X X X X X O O X X

leetcode130.(Medium)Surrounded Regions

解題思路: 從board最邊緣一圈開始向內使用DFS,將可以連通外圈的O設為'#',最後將所有的'#'轉化為'O',將其他的'O'轉化為'X'。 提交程式碼: class Solution{ public void solve(char[][] board) { if(b

Surrounded Regions -- LeetCode

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

leetcode(NOWCODER)---surrounded-regions

時間限制:1秒 空間限制:32768K 熱度指數:17818 本題知識點: 陣列 leetcode 演算法知識視訊講解 題目描述 Given a 2D board containing’X’and’O’, capture all regions surrounded by’X’.

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