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演算法
- 130被圍繞的區域
- 130. 被圍繞的區域
- Python實戰操作:解題之被圍繞的區域Python
- LeetCode 刷題筆記LeetCode筆記
- LeetCode刷題記錄LeetCode
- leetcode刷題筆記LeetCode筆記
- leetcode刷題記錄 661~LeetCode
- leetcode刷題筆記605LeetCode筆記
- leetcode刷題筆記(3)(python)LeetCode筆記Python
- 【Leetcode刷題篇】leetcode56 合併區間LeetCode
- LeetCode刷題筆記9.2-9.9LeetCode筆記
- leetcode刷題筆記8.5-8.9LeetCode筆記
- Leetcode刷題筆記8.12-8.16LeetCode筆記
- LeetCode刷題記錄——day1LeetCode
- LeetCode刷題記錄——day5LeetCode
- LeetCode刷題記錄——day4LeetCode
- LeetCode刷題記錄——day3LeetCode
- LeetCode刷題記錄——day2LeetCode
- LeetCode刷題記13-27. 移除元素LeetCode
- LeetCode-劍指Offer刷題記錄LeetCode
- LeetCode 刷題—樹LeetCode
- LeetCode刷題 堆LeetCode
- LeetCode刷題整理LeetCode
- leetcode刷題(一)LeetCode
- leetcode每日一題刷題記錄(10.26-10.30)LeetCode每日一題
- LeetCode刷題記錄與題解(C++版本)LeetCodeC++
- 刷題記錄:劍指offer+遇到的筆試題+LeetCode筆試LeetCode
- leetcode刷題記錄1041-1050 python版LeetCodePython
- 【刷題日記】leetcode-767 重構字串LeetCode字串
- 【刷題日記】leetcode-493 翻轉對LeetCode
- Leetcode171-190刷題筆記(非困難題)LeetCode筆記
- Leetcode刷題準備LeetCode
- LeetCode刷題—陣列LeetCode陣列
- Leetcode刷題分類LeetCode
- 如何使用leetcode刷題LeetCode
- leetcode刷題.763. 劃分字母區間.每日打卡LeetCode