419. Battleships in a Board
https://leetcode.com/problems/battleships-in-a-board/
/*
* @lc app=leetcode id=419 lang=cpp
*
* [419] Battleships in a Board
*/
// @lc code=start
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
const int M = board.size(), N = M == 0 ? 0 : board[0].size();
int res = 0;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (board[i][j] != 'X') continue;
if (i + 1 < M && board[i + 1][j] == 'X') continue;
if (j + 1 < N && board[i][j + 1] == 'X') continue;
++res;
}
}
return res;
}
};
// @lc code=end
Last updated