Fork me on GitHub

Battleships in a Board

Description

https://leetcode.com/problems/battleships-in-a-board/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int height = board.size();
if (height == 0) return 0;
int weight = board[0].size();
int ret = 0;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < weight; ++j) {
if (board[i][j] == 'X' &&
(i + 1 == height || board[i + 1][j] == '.') &&
(j + 1 == weight || board[i][j + 1] == '.')) //Check if this is right down corner
ret += 1;
}
}
return ret;
}
};