Skip to content

Commit ede04e0

Browse files
authored
Update Battleships in a board.java
1 parent fc6f2c8 commit ede04e0

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

Medium/Battleships in a board.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
class Solution {
2-
public int countBattleships(char[][] board) {
3-
int n = board.length;
4-
if (n == 0) return 0;
5-
int m = board[0].length;
6-
7-
int count = 0;
8-
for (int i=0;i<n;i++) {
9-
for (int j=0;j<m;j++) {
10-
if (board[i][j] == '.') continue;
11-
if (i > 0 && board[i-1][j] == 'X')continue;
12-
if (j > 0 && board[i][j-1] == 'X')continue;
13-
14-
count++;
15-
}
2+
3+
public int countBattleships(char[][] board) {
4+
int count = 0;
5+
int rows = board.length;
6+
int cols = board[0].length;
7+
for (int i = 0; i < rows; i++) {
8+
for (int j = 0; j < cols; j++) {
9+
if (board[i][j] == '.' || (i > 0 && board[i - 1][j] == 'X') || (j > 0 && board[i][j - 1] == 'X')) {
10+
continue;
1611
}
17-
18-
return count;
12+
count++;
13+
}
1914
}
15+
return count;
16+
}
2017
}

0 commit comments

Comments
 (0)