-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountBattleShip.cpp
More file actions
31 lines (31 loc) · 833 Bytes
/
countBattleShip.cpp
File metadata and controls
31 lines (31 loc) · 833 Bytes
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
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int res = 0;
int m = board.size();
if (!m) return 0;
int n = board[0].size();
for (int i = 0 ; i < m;i++ ){
for (int j = 0 ; j < n;){
if (board[i][j] == 'X'){
if (i != m-1){
if (board[i+1][j]=='.')res++;
}
else
res++;
j = findEnd(i,j,board);
}
j++;
}
}
return res;
}
int findEnd(int i,int j, vector<vector<char>>& board){
int n = board[0].size();
int m;
for (m = j+1; m <n; m++){
if (board[i][m] =='.') return m-1;
}
return m;
}
};