LeetCode刷題記112-130. 被圍繞的區域
LeetCode刷題記112
130. 被圍繞的區域
class Solution {
public void search(int i, int j, char[][] board, boolean[][] ct) {
if (i < 0 || i >= board.length) return;
if (j < 0 || j >= board[0].length) return;
if (board[i][j] == 'X' || ct[i][j]) return;
ct[i][j] = true;
search(i - 1, j, board, ct);
search(i + 1, j, board, ct);
search(i, j - 1, board, ct);
search(i, j + 1, board, ct);
}
public void solve(char[][] board) {
if (board.length == 0) return;
boolean[][] ct = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i ++) {
if (board[i][0] == 'O' && !ct[i][0]) {
search(i, 0, board, ct);
}
if (board[i][board[0].length - 1] == 'O' && !ct[i][board[0].length - 1]) {
search(i, board[0].length - 1, board, ct);
}
}
for (int j = 0; j < board[0].length; j ++) {
if (board[0][j] == 'O' && !ct[0][j]) {
search(0, j, board, ct);
}
if (board[board.length - 1][j] == 'O' && !ct[board.length - 1][j]) {
search(board.length - 1, j, board, ct);
}
}
for (int i = 0; i < board.length; i ++) {
for (int j = 0; j < board[i].length; j ++) {
if (!ct[i][j]) board[i][j] = 'X';
}
}
}
}
4/5
112/150
相關文章
- LeetCode-130-被圍繞的區域LeetCode
- leetcode 130被圍繞的區域 回溯演算法LeetCode演算法
- Python實戰操作:解題之被圍繞的區域Python
- LeetCode 刷題筆記LeetCode筆記
- leetcode刷題筆記LeetCode筆記
- LeetCode刷題記錄LeetCode
- leetcode刷題筆記605LeetCode筆記
- leetcode刷題記錄 661~LeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- leetcode刷題筆記(3)(python)LeetCode筆記Python
- LeetCode刷題記錄——day1LeetCode
- LeetCode刷題記錄——day2LeetCode
- LeetCode刷題記錄——day3LeetCode
- LeetCode刷題記錄——day4LeetCode
- LeetCode刷題記錄——day5LeetCode
- LeetCode刷題筆記9.2-9.9LeetCode筆記
- leetcode刷題筆記8.5-8.9LeetCode筆記
- 【Leetcode刷題篇】leetcode56 合併區間LeetCode
- 產品管理圍繞的五個核心問題
- LeetCode-劍指Offer刷題記錄LeetCode
- LeetCode刷題記13-27. 移除元素LeetCode
- LeetCode刷題記錄與題解(C++版本)LeetCodeC++
- LeetCode刷題整理LeetCode
- leetcode刷題(一)LeetCode
- LeetCode刷題 堆LeetCode
- LeetCode 刷題—樹LeetCode
- 刷題記錄:劍指offer+遇到的筆試題+LeetCode筆試LeetCode
- 【刷題日記】leetcode-767 重構字串LeetCode字串
- 【刷題日記】leetcode-493 翻轉對LeetCode
- Leetcode刷題筆記8.12-8.16LeetCode筆記
- LeetCode 刷題指南(一):為什麼要刷題LeetCode
- 如何使用leetcode刷題LeetCode
- leetcode刷題--Majority ElementLeetCode
- leetcode刷題--Count PrimesLeetCode
- leetcode刷題--Reverse BitsLeetCode
- leetcode刷題--Rotate ArrayLeetCode
- leetcode刷題--Count and SayLeetCode
- Leetcode刷題分類LeetCode