-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path419.cpp
More file actions
56 lines (55 loc) · 1.79 KB
/
419.cpp
File metadata and controls
56 lines (55 loc) · 1.79 KB
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
50
51
52
53
54
55
56
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int sum = 0;
int row = board.size();
int col = board[0].size();
for(int i=0;i<row;++i)
{
for(int j=0;j<col;++j)
{
bool flag1 = true, flag2 = true;
if(board[i][j] == 'X')
{
if(i-1>=0 && board[i-1][j] == 'X') { flag1 = false; }
if(j-1>=0 && board[i][j-1] == 'X') { flag2 = false; }
if(flag1 && flag2) { ++sum; }
}
}
}
return sum;
}
};
static auto speedup = [](){
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
__________________________________________________________________________________________________
sample 9568 kb submission
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int nShips = 0;
if(board.size() == 0)
return nShips;
for(int i=0; i<board.size(); i++) {
for(int j=0; j<board[i].size(); j++) {
if(board[i][j] == 'X') {
if(i == 0 && j == 0)
nShips++;
else if(i == 0)
nShips+= board[i][j-1] == '.';
else if(j == 0)
nShips+= board[i-1][j] == '.';
else if(board[i-1][j] == '.' && board[i][j-1] == '.')
nShips++;
}
}
}
return nShips;
}
};
__________________________________________________________________________________________________