We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fc6f2c8 commit ede04e0Copy full SHA for ede04e0
Medium/Battleships in a board.java
@@ -1,20 +1,17 @@
1
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
- }
+
+ public int countBattleships(char[][] board) {
+ int count = 0;
+ int rows = board.length;
+ int cols = board[0].length;
+ for (int i = 0; i < rows; i++) {
+ for (int j = 0; j < cols; j++) {
+ if (board[i][j] == '.' || (i > 0 && board[i - 1][j] == 'X') || (j > 0 && board[i][j - 1] == 'X')) {
+ continue;
16
}
17
18
- return count;
+ count++;
+ }
19
+ return count;
20
0 commit comments