示例 1:
示例 2:
class Solution {public int countBattleships(char[][] board) {int m = board.length;int n = board[0].length;int count = 0;for(int i = 0;ifor(int j = 0;jif((board[i][j] == 'X') && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')){count++;}}}return count;}
}
int countBattleships(char** board, int boardSize, int* boardColSize)
{int m = boardSize;int n = boardColSize[0];int count = 0;for(int i = 0;ifor(int j = 0;jif((board[i][j] == 'X') && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')){count++;}}}return count;
}
class Solution:def countBattleships(self, board: List[List[str]]) -> int:m = len(board)n = len(board[0])count = 0for i in range(0,m):for j in range(0,n):if (board[i][j] == 'X') and (i == 0 or board[i - 1][j] == '.') and (j == 0 or board[i][j - 1] == '.'):count+=1return count
class Solution {
public:int countBattleships(vector>& board) {int m = board.size();int n = board[0].size();int count = 0;for(int i = 0;ifor(int j = 0;jif((board[i][j] == 'X') && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')){count++;}}}return count;}
};
Java语言版
C语言版
Python语言版
C++语言版