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

posted on 2017-04-28 23:14  Sempron2800+  阅读(83)  评论(0编辑  收藏  举报