Leetcode_Python 419 甲板上的战舰
生活随笔
收集整理的這篇文章主要介紹了
Leetcode_Python 419 甲板上的战舰
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解題思路
可以通過戰艦的頭來判斷個數,當一個點上面或者左面試X說明它戰艦中間部分.
將模型分為三個部分分析即可。
代碼
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總結
以上是生活随笔為你收集整理的Leetcode_Python 419 甲板上的战舰的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python nacos注册中心_Nac
- 下一篇: MnistData的读取