The area around which LeetCode 130 is surrounded

Mondo Science Updated on 2024-03-07

Given a two-dimensional matrix, contains:'x'with'o'(letter o).

Find all of them'x'around the area and place all of those areas in'o'with'x'Padding.

Example:

x x x x

x o o x

x x o x

x o x x

After running your function, the matrix becomes:

x x x x

x x x x

x x x x

x o x x

Explanation:

The intervals that are surrounded do not exist on the boundary, in other words, on any boundary'o'None of them will be populated as'x'。Anything that is not on the border, or not on the border'o'Connected'o'will eventually be populated as'x'。If two elements are adjacent horizontally or vertically, they are said to be "connected".

We need to turn all the O's surrounded by X into X, and the title clearly says that all O's on the edge cannot be turned into X.

In fact, we will find that we do not need to become x except for the o at the edge and the o connected to the edge o, everything else needs to become x.

After the above reflection, the problem is transformed into a connected regional problem. Here we need to mark itを of the edge and を which is connected to the edge を。Of course, we can use the extra space to save it, but for this problem, we can mutate completely. In this way, the space complexity will be a little better.

The whole process is shown in the figure:

I willを of the edge and を which is connected to the edge をTagged for"a"

The two-dimensional array DFS solution template conversion problem isConnectivity issuesDirect mutate of original arrays, space-saving language support: js, python3, cpp

* *lc app=leetcode id=130 lang=j**ascript * 130] surrounded regions * convert o and surrounding o to afunction mark(board, i, j, rows, cols) ** param board * return do not return anything, modify board in-place instead. */var solve = function (board) for (let i = 0; i < rows; i++)else if (board[i][j] === "a") return board;};
python code:

class solution: def solve(self, board: list[list[str]])none: """ do not return anything, modify board in-place instead. """If the length or width of the number is less than or equal to 2, you do not need to replace if len(board) < = 2 or len(board[0]) = 2: return row, col = len(board), len(board[0]) def dfs(i, j):"""Depth-first algorithms, if eligible, replace with a and test further, otherwise stop""" if i < 0 or j < 0 or i >= row or j >= col or board[i][j] != 'o': return board[i][j] = 'a'dfs(i - 1, j) dfs(i + 1, j) dfs(i, j - 1) dfs(i, j + 1) start from the periphery for i in range(row): dfs(i, 0) dfs(i, col-1) for j in range(col): dfs(0, j) dfs(row-1, j) finally complete the replacement for i in range(row): for j in range(col):  if board[i][j] =='o': board[i][j] = 'x' elif board[i][j] == 'a': board[i][j] = 'o'
cpp code:

class solution , void dfs(vector> &board, int x, int y) public: void solve(vector>& board) for (int j = 0; j < n; +j) for (auto &row : board)
Complexity analysisTime complexity: $o(row * col)$Space complexity: $o(row * col)$

Related Pages