点击打开链接
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
if(board.empty()) return 0;
int row = board.size(), col = board[0].size();
int count =0;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(board[i][j]=='X'&&(i==0 || board[i-1][j]=='.')&&(j==0 ||board[i][j-1]=='.'))
count+=1;
}
}
return count;
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/4c04d3a995ff1b079c11164c8be083d9.png)