欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Leetcode_Python 419 甲板上的战舰

发布时间:2023/12/29 python 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 甲板上的战舰的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。