Given an m x n matrix board where each cell is a battleship 'X' or empty '.', return the number of the battleships onboard.
Battleships can only be placed horizontally or vertically on board. In other words, they can only be made of the shape 1 x k (1 row, k columns) or k x 1 (k rows, 1 column), where k can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).
A battleship is represented by consecutive ‘X’s horizontally or vertically, and ships are separated by at least one ‘.’ cell. We only count the top-left cell of each ship (a cell that is ‘X’ and has no ‘X’ above or to the left).
classSolution {
public:int countBattleships(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size(), ans =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] !='X') && (j ==0|| board[i][j-1] !='X')) {
ans++;
}
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funccountBattleships(board [][]byte) int {
m, n:= len(board), len(board[0])
ans:=0fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifboard[i][j] =='X'&& (i==0||board[i-1][j] !='X') && (j==0||board[i][j-1] !='X') {
ans++ }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintcountBattleships(char[][] board) {
int m = board.length, n = board[0].length, ans = 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]!='X') && (j == 0 || board[i][j-1]!='X')) {
ans++;
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funcountBattleships(board: Array<CharArray>): Int {
val m = board.size
val n = board[0].size
var ans = 0for (i in0 until m) {
for (j in0 until n) {
if (board[i][j] =='X'&& (i ==0|| board[i-1][j] !='X') && (j ==0|| board[i][j-1] !='X')) {
ans++ }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defcountBattleships(self, board: list[list[str]]) -> int:
m, n = len(board), len(board[0])
ans =0for i in range(m):
for j in range(n):
if board[i][j] =='X'and (i ==0or board[i-1][j] !='X') and (j ==0or board[i][j-1] !='X'):
ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfncount_battleships(board: Vec<Vec<char>>) -> i32 {
let m = board.len();
let n = board[0].len();
letmut ans =0;
for i in0..m {
for j in0..n {
if board[i][j] =='X'&& (i ==0|| board[i-1][j] !='X') && (j ==0|| board[i][j-1] !='X') {
ans +=1;
}
}
}
ans
}
}
classSolution {
public:void dfs(vector<vector<char>>& board, int i, int j) {
int m = board.size(), n = board[0].size();
if (i <0|| i >= m || j <0|| j >= n || board[i][j] !='X') return;
board[i][j] ='.';
dfs(board, i+1, j);
dfs(board, i-1, j);
dfs(board, i, j+1);
dfs(board, i, j-1);
}
intcountBattleships(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size(), ans =0;
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
if (board[i][j] =='X') {
dfs(board, i, j);
ans++;
}
}
}
return ans;
}
};
classSolution {
publicintcountBattleships(char[][] board) {
int m = board.length, n = board[0].length, ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j]=='X') {
dfs(board, i, j);
ans++;
}
}
}
return ans;
}
voiddfs(char[][] board, int i, int j) {
int m = board.length, n = board[0].length;
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j]!='X') return;
board[i][j]='.';
dfs(board, i+1, j);
dfs(board, i-1, j);
dfs(board, i, j+1);
dfs(board, i, j-1);
}
}
classSolution {
funcountBattleships(board: Array<CharArray>): Int {
val m = board.size
val n = board[0].size
fundfs(i: Int, j: Int) {
if (i !in0 until m || j !in0 until n || board[i][j] !='X') return board[i][j] = '.' dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
}
var ans = 0for (i in0 until m) {
for (j in0 until n) {
if (board[i][j] =='X') {
dfs(i, j)
ans++ }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defcountBattleships(self, board: list[list[str]]) -> int:
m, n = len(board), len(board[0])
defdfs(i, j):
if i <0or i >= m or j <0or j >= n or board[i][j] !='X':
return board[i][j] ='.' dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
ans =0for i in range(m):
for j in range(n):
if board[i][j] =='X':
dfs(i, j)
ans +=1return ans
impl Solution {
pubfncount_battleships(board: &mut Vec<Vec<char>>) -> i32 {
fndfs(board: &mut Vec<Vec<char>>, i: i32, j: i32) {
let m = board.len() asi32;
let n = board[0].len() asi32;
if i <0|| i >= m || j <0|| j >= n || board[i asusize][j asusize] !='X' {
return;
}
board[i asusize][j asusize] ='.';
dfs(board, i+1, j);
dfs(board, i-1, j);
dfs(board, i, j+1);
dfs(board, i, j-1);
}
let m = board.len();
let n = board[0].len();
letmut ans =0;
for i in0..m {
for j in0..n {
if board[i][j] =='X' {
dfs(board, i asi32, j asi32);
ans +=1;
}
}
}
ans
}
}