Leetcode_Python 419 甲板上的战舰
生活随笔
收集整理的這篇文章主要介紹了
Leetcode_Python 419 甲板上的战舰
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
解題思路
可以通過(guò)戰(zhàn)艦的頭來(lái)判斷個(gè)數(shù),當(dāng)一個(gè)點(diǎn)上面或者左面試X說(shuō)明它戰(zhàn)艦中間部分.
將模型分為三個(gè)部分分析即可。
代碼
class Solution(object):def countBattleships(self, board):""":type board: List[List[str]]:rtype: int"""count = 0for i in range(len(board)):for j in range(len(board[0])):if board[i][j] == "X":count += 1if i == 0 and j > 0:if board[i][j-1] == "X":count = count - 1elif j == 0 and i > 0:if board[i-1][j] == "X":count = count - 1elif i > 0 or j > 0:if board[i][j-1] == "X" or board[i-1][j] == "X":count = count - 1return count總結(jié)
以上是生活随笔為你收集整理的Leetcode_Python 419 甲板上的战舰的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python nacos注册中心_Nac
- 下一篇: python生成日历_使用python生