419. Battleships in a Board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int countBattleships(char[][] board) {
int m = board.length, n = board[0].length;

int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && board[i - 1][j] == 'X') {
continue;
}
if (j > 0 && board[i][j - 1] == 'X') {
continue;
}
if (board[i][j] == 'X') {
// 此点为左上角
count++;
}
}
}

return count;
}
}

References

419. Battleships in a Board