赞
踩
示例 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;i<m;i++){
for(int j = 0;j<n;j++){
if((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;i<m;i++) { for(int j = 0;j<n;j++) { if((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 = 0
for 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+=1
return count
class Solution { public: int countBattleships(vector<vector<char>>& board) { int m = board.size(); int n = board[0].size(); int count = 0; for(int i = 0;i<m;i++) { for(int j = 0;j<n;j++) { if((board[i][j] == 'X') && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) { count++; } } } return count; } };
Java语言版
C语言版
Python语言版
C++语言版
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。