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
- 下一篇: python生成日历_使用python生