-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBattleshipsInABoard.java
49 lines (48 loc) · 1.5 KB
/
BattleshipsInABoard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*https://leetcode.com/problems/battleships-in-a-board/*/
class Solution {
public int countBattleships(char[][] board) {
int battleshipCount = 0;
int R = board.length, C = board[0].length;
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
int hvInd = 0;
if (board[i][j] == 'X')
{
if (i+1 < R && board[i+1][j] == 'X') hvInd = 1;
else if (j+1 < C && board[i][j+1] == 'X') hvInd = 2;
else hvInd = -1;
}
if (hvInd == -1)
{
board[i][j] = '.';
++battleshipCount;
}
else if (hvInd != 0)
{
if (hvInd == 1)
{
int r = i, c = j;
while (r < R && board[r][c] == 'X')
{
board[r][c] = '.';
++r;
}
}
else
{
int r = i, c = j;
while (c < C && board[r][c] == 'X')
{
board[r][c] = '.';
++c;
}
}
++battleshipCount;
}
}
}
return battleshipCount;
}
}