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. java-w3c.document生成xml文件

    案例 /** * 创建和写入xml * @param xmlrootname * @param waitConverList */ private void createAndWriterXML(St ...

  2. Java学习笔记(二)

    再次更新,有问题大家一起讨论 Java中的class文件转换成.java文件 下载转换器Java Decompiler 使用此转换器打开.jar文件 就可以看到由.class转换来的.java文件 亲 ...

  3. 在SpringMVC中使用@SessionAttributes和@ModelAttribute将数据存储在session域中

    今天在我的springMVC项目--图书管理系统中,希望在登录时将登录的Users存在session中,开始是准备在controller中使用Servlet API中的对象,可是一直无法引用,不知道为 ...

  4. c# Entity DbArithmeticExpression arguments must have a numeric common type

    在Entity Lambda表达式中计算DateTime类型的时候 不能直接用DateTime类型进行相减计算差值 需要使用DbFunctions提供的一些方法. 例如: DbFunctions.Di ...

  5. ffmpeg获取文件的总时长(mp3/mp4/flv等)

    使用ffmpeg.exe获取文件属性信息,C#中可以在进程外异步调用这个工具,如下: using (System.Diagnostics.Process pro = new System.Diagno ...

  6. 帝国cms内容页调用缩略图的原始尺寸图片

    在发布文章上传标题图片时,勾选"生成缩略图",将生成原图和对应的缩略图 原图的链接为[!--titlepic--]:/d/file/anlizhanshi/2016-11-25/8 ...

  7. Beta阶段发布说明

    OverWatch来浪狼人杀助手Beta版本发布说明 Beta版本的新功能 添加了忘记密码与重置密码的功能 添加了语音流程提示的功能 添加了搜索好友的功能 添加了添加好友的功能 添加了能够直接通过点击 ...

  8. UVA227

    步骤:1.把输入的数字和空格保存.(这里用到gets函数读取整行)2.定位空格.3.输入指令. #include<stdio.h> #include<string.h> ][] ...

  9. zlib-1.2.7/libpng-1.5.9 instead of zlib-1.2.8/libpng-1.6.6

    The reason for the failure apparently appears to be version incompatibility, partly may be due to li ...

  10. Android Studio如何减小APK体积

    最近在用AndroidStudio开发一个小计算器,代码加起来还不到200行.但是遇到一个问题,导出的APK文件大小竟然达到了1034K.这不科学,于是就自己动手精简APK.下面我们大家一起学习怎么缩 ...