python leetcode 419. Battleships in a Board

无难度 按照题意解题即可
按照遍历顺序只需判断左边和上边是否相连

class Solution:
    def countBattleships(self, board):
        """
        :type board: List[List[str]]
        :rtype: int
        """
        m=len(board)
        if m==0: return 0
        n=len(board[0])
        count=0
        for i in range(m):
            for j in range(n):
                if board[i][j]=='X':
                    if (i-1<0 or board[i-1][j]=='.') and (j-1<0 or board[i][j-1]=='.'):
                        count+=1
        return count

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84646927
今日推荐