1
1
package com .fishercoder .solutions ;
2
2
3
- /**Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:
3
+ /**
4
+ * 419. Battleships in a Board
5
+ *
6
+ * Given an 2D board, count how many battleships are in it.
7
+ * The battleships are represented with 'X's, empty slots are represented with '.'s.
8
+ * You may assume the following rules:
4
9
5
10
You receive a valid board, made of only battleships or empty slots.
6
- Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
11
+ Battleships can only be placed horizontally or vertically.
12
+ In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
7
13
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
14
+
8
15
Example:
9
16
10
17
X..X
27
34
*/
28
35
public class _419 {
29
36
30
- /**
31
- * credit: https://discuss.leetcode.com/topic/62970/simple-java-solution,
32
- * basically, it only counts the top-left one while ignoring all other parts of one battleship,
33
- * using the top-left one as a representative for one battle.
34
- * This is achieved by counting cells that don't have 'X' to the left and above them.
35
- */
36
- public int countBattleships_no_modify_original_input (char [][] board ) {
37
- if (board == null || board .length == 0 ) {
38
- return 0 ;
39
- }
40
- int count = 0 ;
41
- int m = board .length ;
42
- int n = board [0 ].length ;
43
- for (int i = 0 ; i < m ; i ++) {
44
- for (int j = 0 ; j < n ; j ++) {
45
- if (board [i ][j ] == '.' ) {
46
- continue ;//if it can pass this line, then board[i][j] must be 'X'
47
- }
48
- if (j > 0 && board [i ][j - 1 ] == 'X' ) {
49
- continue ;//then we check if its left is 'X'
50
- }
51
- if (i > 0 && board [i - 1 ][j ] == 'X' ) {
52
- continue ;//also check if its top is 'X'
37
+ public static class Solution1 {
38
+ /**
39
+ * credit: https://discuss.leetcode.com/topic/62970/simple-java-solution,
40
+ * <p>
41
+ * This solution does NOT modify original input.
42
+ * Basically, it only counts the top-left one while ignoring all other parts of one battleship,
43
+ * using the top-left one as a representative for one battle.
44
+ * This is achieved by counting cells that don't have 'X' to the left and above them.
45
+ */
46
+ public int countBattleships (char [][] board ) {
47
+ if (board == null || board .length == 0 ) {
48
+ return 0 ;
49
+ }
50
+ int count = 0 ;
51
+ int m = board .length ;
52
+ int n = board [0 ].length ;
53
+ for (int i = 0 ; i < m ; i ++) {
54
+ for (int j = 0 ; j < n ; j ++) {
55
+ if (board [i ][j ] == '.' ) {
56
+ continue ;//if it can pass this line, then board[i][j] must be 'X'
57
+ }
58
+ if (j > 0 && board [i ][j - 1 ] == 'X' ) {
59
+ continue ;//then we check if its left is 'X'
60
+ }
61
+ if (i > 0 && board [i - 1 ][j ] == 'X' ) {
62
+ continue ;//also check if its top is 'X'
63
+ }
64
+ count ++;
53
65
}
54
- count ++;
55
66
}
67
+ return count ;
56
68
}
57
- return count ;
58
69
}
59
70
60
- /**
61
- * My original solution, actually modified the input. I just undo it at the end.
62
- */
63
- public int countBattleships (char [][] board ) {
64
- if (board == null || board .length == 0 ) {
65
- return 0 ;
66
- }
67
- int result = 0 ;
68
- int m = board .length ;
69
- int n = board [0 ].length ;
70
- for (int i = 0 ; i < m ; i ++) {
71
- for (int j = 0 ; j < n ; j ++) {
72
- if (board [i ][j ] == 'X' ) {
73
- result ++;
74
- dfs (board , i , j , m , n );
71
+ public static class Solution2 {
72
+ /**
73
+ * My original solution, actually modified the input. I just undo it at the end.
74
+ */
75
+ public int countBattleships (char [][] board ) {
76
+ if (board == null || board .length == 0 ) {
77
+ return 0 ;
78
+ }
79
+ int result = 0 ;
80
+ int m = board .length ;
81
+ int n = board [0 ].length ;
82
+ for (int i = 0 ; i < m ; i ++) {
83
+ for (int j = 0 ; j < n ; j ++) {
84
+ if (board [i ][j ] == 'X' ) {
85
+ result ++;
86
+ dfs (board , i , j , m , n );
87
+ }
75
88
}
76
89
}
77
- }
78
90
79
- for (int i = 0 ; i < m ; i ++) {
80
- for (int j = 0 ; j < n ; j ++) {
81
- if (board [i ][j ] == '#' ) {
82
- board [i ][j ] = 'X' ;
91
+ for (int i = 0 ; i < m ; i ++) {
92
+ for (int j = 0 ; j < n ; j ++) {
93
+ if (board [i ][j ] == '#' ) {
94
+ board [i ][j ] = 'X' ;
95
+ }
83
96
}
84
97
}
98
+ return result ;
85
99
}
86
- return result ;
87
- }
88
100
89
- private void dfs (char [][] board , int x , int y , int m , int n ) {
90
- if (x < 0 || x >= m || y < 0 || y >= n || board [x ][y ] != 'X' ) {
91
- return ;
92
- }
93
- if (board [x ][y ] == 'X' ) {
94
- board [x ][y ] = '#' ;
101
+ private void dfs (char [][] board , int x , int y , int m , int n ) {
102
+ if (x < 0 || x >= m || y < 0 || y >= n || board [x ][y ] != 'X' ) {
103
+ return ;
104
+ }
105
+ if (board [x ][y ] == 'X' ) {
106
+ board [x ][y ] = '#' ;
107
+ }
108
+ dfs (board , x + 1 , y , m , n );
109
+ dfs (board , x , y + 1 , m , n );
110
+ dfs (board , x - 1 , y , m , n );
111
+ dfs (board , x , y - 1 , m , n );
95
112
}
96
- dfs (board , x + 1 , y , m , n );
97
- dfs (board , x , y + 1 , m , n );
98
- dfs (board , x - 1 , y , m , n );
99
- dfs (board , x , y - 1 , m , n );
100
113
}
101
114
102
- public static void main (String ... strings ) {
103
- char [][] board = new char [][]{
104
- {'X' , '.' , '.' , 'X' },
105
- {'.' , '.' , '.' , 'X' },
106
- {'.' , '.' , '.' , 'X' },
107
- };
108
-
109
- _419 test = new _419 ();
110
- System .out .println (test .countBattleships (board ));
111
- }
112
- }
115
+ }
0 commit comments