https://leetcode.com/problems/battleships-in-a-board/

给定一个N×N的棋盘,有任意数量的1×N或N×1大小的“船”,注意船船之间是不相邻的,要求统计有多少船

Example:

X..X
...X
...X

In the above board there are 2 battleships.

解题思路

Given an 2D board, count how many different battleships are in it.

这是题目头一句话,different这个词把我搞懵了,我的理解是如果在棋盘上如果有同样size,同样方向的船应该是算一种的

如["XX..","....","..XX"]这个例子,输出我觉得应该是1,但是我看了leetcode给的top solutions,按照这类解法,输出普遍是2

rucode了一下,结果如下:

Run Code Result:
Your input
["XX..","....","..XX"]
Expected answer
2
 
Runtime: 39 ms

说明其实只要统计棋盘里面有几条船就可以了

那么解法就很简单了,只用统计有多少个“船头”就可以了

船头定义如下:

1、是个X(废话)

2、上面为空(在棋盘边界)或为.

3、左边为空(在棋盘边界)或为.

同时注意测试用例其实是没有空棋盘(return0)的情况的,加上判空代码是处于严谨性

船头解法

 class Solution(object):#head,top_left
def countBattleships(self, board):
if len(board) == 0: return 0
m, n = len(board), len(board[0])
count = 0
for i in range(m):
for j in range(n):
if board[i][j] == 'X' and (i == 0 or board[i - 1][j] == '.') and (j == 0 or board[i][j - 1] == '.'):
count += 1
return count

按照同样的思路,你也可以统计船尾,左上改为右下即可

if board[i][j] == 'X' and (i == m-1 or board[i + 1][j] == '.') and (j == n-1 or board[i][j + 1] == '.')

419. Battleships in a Board的更多相关文章

  1. 【LeetCode】419. Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  2. 419. Battleships in a Board 棋盘上的战舰数量

    [抄题]: Given an 2D board, count how many battleships are in it. The battleships are represented with  ...

  3. [LeetCode] 419. Battleships in a Board 平板上的战船

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  4. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. LeetCode "419. Battleships in a Board"

    The follow-up question is fun: "Could you do it in one-pass, using only O(1) extra memory and w ...

  6. 419 Battleships in a Board 甲板上的战舰

    给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则:    给你一个有效的甲板,仅由战舰或者空位组成.    战舰只能水平或者垂直放置.换句话 ...

  7. [LeetCode] Battleships in a Board 平板上的战船

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  8. Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  9. Leetcode: Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

随机推荐

  1. oracle数据泵导入

    假设将dmp放到/data目录下,首先在数据库中创建directory目录SQL> create directory exp as '/data/'在操作系统命令执行导入命令.impdp sys ...

  2. [翻译]ES 提案: global

    Jordan Harband 的 ECMAScript 提案“global”现在处于第三阶段.该提案提供了一种新的用于访问全局对象的标准方式. 全局对象的引用 下面是常用的几种引用全局对象的方式: 全 ...

  3. sql语句-备忘

    -- 清空全部数据,不写日志,不可恢复,速度极快truncate table 表名; -- 清空全部数据,写日志,数据可恢复,速度慢delete from 表名;   --从一个数据库复制一张表带另一 ...

  4. 利用django创建一个投票网站(四)

    创建你的第一个 Django 项目, 第四部分 这一篇从第三部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,专注于简单的表单处理并且精简我们的代码. 编写一个简单的表单 让我们更新一下在上一个 ...

  5. 问题记录:JavaFx 鼠标滑轮滚动事件监听!

    问题描述: 在listview的item里面添加鼠标拖拽排序功能.代码如下: setOnMouseDragged(event -> { //设定鼠标长按0.3秒后才可拖拽 防止误操作 isCan ...

  6. Sublime Text 配置代码

    { "font_face": "Courier New bold", "font_size": 15.0, "ignored_wo ...

  7. c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享

      string sql = @"select * from T_Excel"; ----------------DataTable Star----------------    ...

  8. sublime 中 pyv8 binary 报错怎么处理?

    ,sublime text 在安装插件的时候,尤其是Emmet的时候,经常会报一个错误--please wait a bit whilePyV8 binary is being downloaded, ...

  9. c++ 在windows下获取时间和计算时间差的几种方法总结

    http://blog.csdn.net/caimagic/article/details/50696609 我用的是GetTickCount(), 获取到的是毫秒.

  10. SQL Sever无法打开链接对话框,未将对象引用设置到对象的实例。(AppIDPackage)

    前几天刚做完系统,先装的是SQL Sever2008,装完后还试了一下,OK~没问题,然后就继续装VS2012等一些软件.搞到很晚没有继续试试就睡了,第二天运行SSMS出问题了..(如图 1.0 所示 ...