FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solution/C++/surrounded-regions.cpp at master · paperfactory/LeetCode-Solution · GitHub
paperfactory
LeetCode-Solution
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solution
/
C++
/
surrounded-regions.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (47 loc) · 1.51 KB
Breadcrumbs
LeetCode-Solution
/
C++
/
surrounded-regions.cpp
Copy path
File metadata and controls
51 lines (47 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
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) {
q.
emplace
(i,
0
);
q.
emplace
(i, board[
0
].
size
() -
1
);
}
for
(
int
j =
0
; j < board[
0
].
size
(); ++j) {
q.
emplace
(
0
, j);
q.
emplace
(board.
size
() -
1
, j);
}
while
(!q.
empty
()) {
int
i, j;
tie
(i, j) = q.
front
();
q.
pop
();
if
(board[i][j] ==
'
O
'
|| board[i][j] ==
'
V
'
) {
board[i][j] =
'
V
'
;
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
'
;
}
}
}
}
};
Back
|
FazBrowse Home
|
New Git URL