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
网友评论