Skip to content

Commit 15bb686

Browse files
committed
Create 419. 甲板上的战舰.js
1 parent 98282a0 commit 15bb686

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

419. 甲板上的战舰.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {number}
4+
*/
5+
var countBattleships = function (board) {
6+
let result = 0;
7+
for (let i = 0; i < board.length; i++) {
8+
for (let j = 0; j < board[i].length; j++) {
9+
if (
10+
board[i][j] === 'X' &&
11+
board[i][j - 1] !== 'X' &&
12+
(!board[i - 1] || board[i - 1][j] !== 'X')
13+
) {
14+
result++;
15+
}
16+
}
17+
}
18+
return result;
19+
};

0 commit comments

Comments
 (0)