美文网首页
419. Battleships in a Board

419. Battleships in a Board

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-21 09:41 被阅读0次
class Solution(object):
    def countBattleships(self, board):
        """
        :type board: List[List[str]]
        :rtype: int
        """
        if not board or len(board[0])==0: return 0
        
        count=1 if board[0][0]=='X' else 0
        
        for i in range(1,len(board)):
            if board[i][0]=='X' and board[i-1][0]=='.':
                count+=1
        for j in range(1,len(board[0])):
            if board[0][j]=='X' and board[0][j-1]=='.':
                count+=1
        for i in range(1,len(board)):
            for j in range(1,len(board[0])):
                if board[i][j]=='X' and board[i][j-1]=='.' \
                and board[i-1][j]=='.':
                    count+=1
        return count 

相关文章

网友评论

      本文标题:419. Battleships in a Board

      本文链接:https://www.haomeiwen.com/subject/qpafvttx.html