Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
surrounded-regions.cpp 1.80 KB
Copy Edit Raw Blame History
kamyu authored 2019-01-19 22:20 +08:00 . Update surrounded-regions.cpp
// Time: O(m * n)
// Space: O(m + n)
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.empty()) {
return;
}
queue<pair<int, int>> q;
for (int i = 0; i < board.size(); ++i) {
if (board[i][0] == 'O') {
board[i][0] = 'V';
q.emplace(i, 0);
}
if (board[i][board[0].size() - 1] == 'O') {
board[i][board[0].size() - 1] = 'V';
q.emplace(i, board[0].size() - 1);
}
}
for (int j = 1; j < board[0].size() - 1; ++j) {
if (board[0][j] == 'O') {
board[0][j] = 'V';
q.emplace(0, j);
}
if (board[board.size() - 1][j] == 'O') {
board[board.size() - 1][j] = 'V';
q.emplace(board.size() - 1, j);
}
}
while (!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
static const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
for (const auto& d : directions) {
const int x = i + d.first, y = j + d.second;
if (0 <= x && x < board.size() &&
0 <= y && y < board[0].size() &&
board[x][y] == 'O') {
board[x][y] = 'V';
q.emplace(x, y);
}
}
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
if (board[i][j] != 'V') {
board[i][j] = 'X';
} else {
board[i][j] = 'O';
}
}
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search