Skip to content

Latest commit

 

History

History
19 lines (18 loc) · 484 Bytes

419. Battleships in a Board.md

File metadata and controls

19 lines (18 loc) · 484 Bytes
class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int cnt = 0;
        for (int i = 0; i < board.size(); ++i) {
            for (int j = 0; j < board[0].size(); ++j) {
                if (board[i][j] != '.') {
                    if ((i == 0 || board[i - 1][j] != 'X') &&
                       (j == 0 || board[i][j - 1] != 'X'))
                        ++cnt;
                }
            }
        }
        return cnt;
    }
};