Available Captures for Rook LeetCode solution in Java

1
class Solution {
2
public int numRookCaptures(char[][] board) {
3
int ans = 0;
4

5
int row = 0;
6
int col = 0;
7
for (int i = 0; i < 8; i++) {
8
for (int j = 0; j < 8; j++) {
9
if (board[i][j] == 'R') {
10
row = i;
11
col = j;
12
break;
13
}
14
}
15
}
16

17
int j = col;
18
while (j >= 0) {
19
if (board[row][j] == 'B') {
20
break;
21
} else if (board[row][j] == 'p') {
22
ans++;
23
break;
24
}
25

26
j--;
27
}
28

29
j = col;
30
while (j <= board[0].length - 1) {
31
if (board[row][j] == 'B') {
32
break;
33
} else if (board[row][j] == 'p') {
34
ans++;
35
break;
36
}
37

38
j++;
39
}
40

41
int i = row;
42
while (i <= board.length - 1) {
43
if (board[i][col] == 'B') {
44
break;
45
} else if (board[i][col] == 'p') {
46
ans++;
47
break;
48
}
49

50
i++;
51
}
52

53
i = row;
54
while (i >= 0) {
55
if (board[i][col] == 'B') {
56
break;
57
} else if (board[i][col] == 'p') {
58
ans++;
59
break;
60
}
61

62
i--;
63
}
64

65
return ans;
66
}
67
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0