目录
小前言
思路:
上代码
lucky ending
小前言
经过漫长的停更周期-----1个月
我决定铁血回归!!!
思路:
两层for循环暴力最快了这种小小范围题,主要是第一行和第一列的边界处理,我分为左上角,第一行,第一列,一般情况来处理。对于一般情况的总体判断思路,采取当前为X时,左边和上边都为 . 那么num+1
上代码
class Solution(object):
def countBattleships(self, board):
"""
:type board: List[List[str]]
:rtype: int
"""
m=len(board)
n=len(board[0])
num=0
for i in range(m):
for j in range(n):
if i==0 and j==0 and board[i][j]=="X":
num+=1
continue
if i==0 and board[i][j]=="X" and board[i][j-1]==".":
num+=1
continue
if j==0 and board[i][j]=="X" and board[i-1][j]==".":
num+=1
continue
if board[i][j]=="X" and board[i-1][j]=="." and board[i][j-1]==".":
num+=1
return num
lucky ending