Skip to content

Commit 98e761e

Browse files
committed
Add solution #419
1 parent efb0682 commit 98e761e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
383|[Ransom Note](./0383-ransom-note.js)|Easy|
137137
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
138138
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
139+
419|[Battleships in a Board](./0419-battleships-in-a-board.js)|Medium|
139140
442|[Find All Duplicates in an Array](./0442-find-all-duplicates-in-an-array.js)|Medium|
140141
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
141142
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 419. Battleships in a Board
3+
* https://leetcode.com/problems/battleships-in-a-board/
4+
* Difficulty: Medium
5+
*
6+
* Given an m x n matrix board where each cell is a battleship 'X' or empty '.', return the
7+
* number of the battleships on board.
8+
*
9+
* Battleships can only be placed horizontally or vertically on board. In other words, they
10+
* can only be made of the shape 1 x k (1 row, k columns) or k x 1 (k rows, 1 column), where
11+
* k can be of any size. At least one horizontal or vertical cell separates between two
12+
* battleships (i.e., there are no adjacent battleships).
13+
*/
14+
15+
/**
16+
* @param {character[][]} boardx
17+
* @return {number}
18+
*/
19+
var countBattleships = function(board) {
20+
let count = 0;
21+
22+
for (let i = 0; i < board.length; i++) {
23+
for (let j = 0; j < board[i].length; j++) {
24+
if (board[i][j] === 'X' && board[i][j - 1] !== 'X' && (!board[i - 1] || board[i - 1][j] !== 'X')) {
25+
count++;
26+
}
27+
}
28+
}
29+
30+
return count;
31+
};

0 commit comments

Comments
 (0)