https://leetcode.com/problems/battleships-in-a-board/description/
int countBattleships(vector<vector<char>>& board) {
int res = 0;
for(int i=0; i<board.size(); i++)
for(int j=0; j<board[0].size(); j++) {
if(board[i][j] == 'X') {
if(i!=0 && j!=0)
res += (board[i-1][j] != 'X' && board[i][j-1] != 'X');
else if(i==0 && j!=0)
res += (board[i][j-1] != 'X');
else if(i!=0 && j==0)
res += (board[i-1][j] != 'X');
else res++;
}
}
return res;
}
No comments:
Post a Comment