419. Battleships in a Board Medium

@problem@discussion
#Array#Depth-First Search#Matrix



1/**
2 * [419] Battleships in a Board
3 *
4 * Given an m x n matrix board where each cell is a battleship 'X' or empty '.', return the number of the battleships on board.
5 * Battleships can only be placed horizontally or vertically on board. In other words, they can only be made of the shape 1 x k (1 row, k columns) or k x 1 (k rows, 1 column), where k can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/battelship-grid.jpg" style="width: 333px; height: 333px;" />
9 * Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
10 * Output: 2
11 * 
12 * Example 2:
13 * 
14 * Input: board = [["."]]
15 * Output: 0
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	m == board.length
21 * 	n == board[i].length
22 * 	1 <= m, n <= 200
23 * 	board[i][j] is either '.' or 'X'.
24 * 
25 *  
26 * Follow up: Could you do it in one-pass, using only O(1) extra memory and without modifying the values board?
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/battleships-in-a-board/
32// discuss: https://leetcode.com/problems/battleships-in-a-board/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn count_battleships(board: Vec<Vec<char>>) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_419() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.