forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0130-surrounded-regions.ts
48 lines (42 loc) · 1.14 KB
/
0130-surrounded-regions.ts
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
/**
Do not return anything, modify board in-place instead.
*/
functionsolve(board: string[][]): void{
constrowLen=board.length;
constcolLen=board[0].length;
constlastRow=rowLen-1;
constlastCol=colLen-1;
for(letr=0;r<rowLen;++r){
markSeen(r,0);
markSeen(r,lastCol);
}
for(letc=1;c<lastCol;++c){
markSeen(0,c);
markSeen(lastRow,c);
}
for(letr=0;r<rowLen;++r){
for(letc=0;c<colLen;++c){
switch(board[r][c]){
case'O':
board[r][c]='X';
break;
case'A':
board[r][c]='O';
break;
}
}
}
functionmarkSeen(r: number,c: number): void{
if(!inBounds(r,c)||board[r][c]!=='O'){
return;
}
board[r][c]='A';
markSeen(r-1,c);
markSeen(r+1,c);
markSeen(r,c-1);
markSeen(r,c+1);
}
functioninBounds(r: number,c: number): boolean{
returnr>=0&&c>=0&&r<rowLen&&c<colLen;
}
};