思路:找到战船的起始点即可,起始点即左边,上边非’X’的点。
class Solution(object):
def countBattleships(self, board):
"""
:type board: List[List[str]]
:rtype: int
"""
count = 0
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 'X':
if i > 0 and board[i-1][j] == 'X':
continue
if j > 0 and board[i][j-1] == 'X':
continue
count += 1
return count