419.甲板上的战舰
解法一:
class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
ship = 0 #战舰数
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == '.': #空位
continue
if i-1 >= 0 and board[i-1][j] == 'X': #上方有X
continue
if j-1 >= 0 and board[i][j-1] == 'X': #左侧有X
continue
ship += 1 #上方和左侧都没有X的,为最左上角
return ship解法二:并查集
最后更新于