思路:
遍历数组,对于每个'X',判断其左边和上边是否有'X',若没有,则结果+1.
class Solution { public int countBattleships(char[][] board) { int res=0; int width=board.length; int height=board[0].length; for(int i=0;i<width;i++){ for(int j=0;j<height;j++){ char point=board[i][j]; if(point=='X'){ if(i>0 && board[i-1][j]=='X'){ continue; } if(j>0 && board[i][j-1]=='X'){ continue; } res++; } } } return res; } }