leetcode——surronded regions

时间复杂度: O(n)

空间复杂度:O(n)

题意解析:

就是把所有被“x”包围的“o”替换成 x。所谓 “包围” 是指 上下左右 四个方向,不包括斜上,斜下。。。

算法思路:

没有被“x” 围住: 就是 那一组联通的“o“ 连接到边界了,只要把连接到边界的 ”o“ 替换成*,其他的o就替换成x,最后再把*还原成O

在把连接到边界的O替换成* 用的是bfs,具体代码如下,time:52ms

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

class Solution {
public:
	void solve(vector<vector<char>> &board) {
		int height = board.size(), width;
		if (height == 0)
			return;
		width = board[0].size();
		//search the 2 colum (the first colum and  the last colum)
		for (int i = 0; i < height; i++){
			if (board[i][0] == 'O')
				bfs(i, 0, board);
			if (board[i][width - 1] == 'O')
				bfs(i, width - 1, board);
		}
		//search the first and last rows
		for (int i = 1; i < width - 1; i++){
			if (board[0][i] == 'O')
				bfs(0, i, board);
			if (board[height - 1][i] == 'O')
				bfs(height - 1, i, board);
		}
		for (int i = 0; i < height; i++){
			for (int j = 0; j < width; j++){
				if (board[i][j] == '*'){
					board[i][j] = 'O';
				}
				if (board[i][j] == 'O'){
					board[i][j] = 'X';
				}
			}
		}
	}

	private:
	struct position{
		int x;
		int y;
	};
	void bfs(int x, int y, vector<vector<char>> &board){
		queue<position>	q_pos;
		visit(x, y, board, q_pos);
		while (!q_pos.empty()){
			auto next_pos = q_pos.front();
			q_pos.pop();
			visit(x - 1, y, board, q_pos);
			visit(x + 1, y, board, q_pos);
			visit(x, y + 1, board, q_pos);
			visit(x, y - 1, board, q_pos);
		}
	}
	void visit(int x, int y, vector<vector<char>> &board, queue<position> &q_pos){
		if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || board[x][y] == 'X')
			return;
		if (board[x][y] == 'O'){
			board[x][y] = '*';
			position p;
			p.x = x;
			p.y = y;
			q_pos.push(p);
		}
	}
};
时间: 2024-08-28 17:32:41

leetcode——surronded regions的相关文章

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

[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

验证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]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 解题报告

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 —— Surrounded Regions

Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium 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

[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

[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

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