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 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

Have you been asked this question in an interview?

class Solution {
 	private static int rows = 0;
	private static int cols = 0;
	private static Queue<Integer> queue = null;
	private static char[][] myboard;

	public void solve(char[][] board) {
		if (board == null) {
			return;
		}
		if (board.length == 0 || board[0].length == 0) {
			return;
		}
		myboard = board;
		queue = new LinkedList<Integer>();
		rows = myboard.length;
		cols = myboard[0].length;
		for (int i = 0; i < rows; i++) {
			putQueue(0,i);
			putQueue(cols - 1,i);
		}
		for (int j = 1; j < cols - 1; j++) {
			putQueue(j,0);
			putQueue(j,rows - 1);
		}
		while (!queue.isEmpty()) {
			int position = queue.poll();
			int x = position % cols, y = position / cols;
			if (myboard[y][x] == 'O') {
				myboard[y][x] = 'D';
			}
			putQueue(x - 1, y);
			putQueue(x + 1, y);
			putQueue(x, y - 1);
			putQueue(x, y + 1);

		}
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (myboard[i][j] == 'O') {
					myboard[i][j] = 'X';
				} else if (myboard[i][j] == 'D') {
					myboard[i][j] = 'O';
				}
			}
		}

	}

	public void putQueue(int x, int y) {
		if (0 <= x && x < cols && 0 <= y && y < rows && myboard[y][x] == 'O') {
			queue.offer(y * cols + x);
		}
	}
}

这个题目,有个东西 那个是O 而不是0!!!!!!!!!!。。。。。

另外计算xy的位置的时候,不要弄错了

时间: 2024-10-27 01:30:23

Surrounded Regions LeetCode :My Solution的相关文章

Surrounded Regions leetcode java

题目: 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 sho

Surrounded Regions -- leetcode

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

Surrounded Regions——LeetCode

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

【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周的一定不是

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

验证LeetCode Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =

LeetCode[BFS]: 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

[leetcode]Surrounded Regions @ Python

原题地址:https://oj.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

[LeetCode] Surrounded Regions(DFS、BFS)

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