150強訪談簡單

週邊地區

「週邊區域」問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個包含「X」和「O」的 m x n 矩陣板,捕捉所有由「X」在 4 方向包圍的區域。

透過將包圍區域中的所有“O”翻轉為“X”來捕捉區域。

寫一個函數 solve(board: List[List[str]]) -> List[List[str]] 傳回修改後的板。

約束條件
  • m == len(board)
  • n == len(board[i])
  • 1 <= m, n <= 200
  • board[i][j] is 'X' or 'O'

範例

Example 1
Input
board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output
[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Explanation

Surrounded regions should not be on the border, which means any 'O' on the border of the board is not flipped to 'X'. Any 'O' that is connected to a border 'O' is also not flipped.

Need a Hint?
考慮使用特定於圖的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。