public class Solution { public int CountBattleships(char[,] board) { var row = board.GetLength(0);//3行 var col = board.GetLength(1);//4列 int count = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i, j] == 'X' && (i == 0 || board[i - 1, j] != 'X') && (j == 0 || board[i, j - 1] != 'X')) { count++; } } } return count; } }
https://leetcode.com/problems/battleships-in-a-board/#/description