419. Battleships in a Board
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的更多相关文章
- 【LeetCode】419. Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- 419. Battleships in a Board 棋盘上的战舰数量
[抄题]: Given an 2D board, count how many battleships are in it. The battleships are represented with ...
- [LeetCode] 419. Battleships in a Board 平板上的战船
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...
- 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 419 Battleships in a Board 甲板上的战舰
给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句话 ...
- [LeetCode] Battleships in a Board 平板上的战船
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- Leetcode: Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
随机推荐
- VLC 资料整理
libvlc_media_t的创建 创建libvlc_media_t有两种方法:libvlc_media_new_path()和libvlc_media_new_location().简单描述一下这两 ...
- Android 解析聊天表情的笔记
应用需要用到聊天功能,考虑到开始需求不大,暂时先用第三方的. 一研究发现界面风格有点不符合整体的风格,加上需要一些自己的特定的需求和界面显示,于是就决定调用第三方数据接口,界面自己写.功能只需要文字, ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
- Python json.dumps 特殊数据类型的自定义序列化操作
场景描述: Python标准库中的json模块,集成了将数据序列化处理的功能:在使用json.dumps()方法序列化数据时候,如果目标数据中存在datetime数据类型,执行操作时, 会抛出异常:T ...
- javascript 容易忽略的小知识点 new到底做了什么?
问题:平时我们经常写 var ss = new Person():ss就是一个由'Person类'生成的对象了,可是我们的Person方法里却没有写 return: (var ss= Person() ...
- Git,non-fast-forward
当把coding好的code,push到Git时会出现这个错误:master[rejected non-fast-forward] 问题(Non-fast-forward)的出现原因在于:git仓库 ...
- HTML之JS学习
提示篇 function fun(){ var is = confirm('选择对话框');/*确定取消对话框*/ if(is == true){ document.write('真');/*网页输出 ...
- Selenium+python 配置
1. 安装python, www.python.org. 下载最新的python,应该是32位的.注意配置环境变量. 2. 安装PIP(pip是一个以Python计算机程序语言写成的软件包管理系统). ...
- vim的编译安装及其插件YouCompleteMe安装
相关的环境: win 7 x64 vs2013 community python 2.7.10 AMD64 python 3.5 AMD64 LLVM 3.5 cmake 3.5 YouCompl ...
- ubuntu 12.04 LTS 64位兼容运行32位程序
安装完Goagent,运行的时候出现了问题,在网络上翻看一些关于ubuntu的文档时,突然记起自己安装的是64位版,而goagent应该是32位的,于是通过sudo apt-get install i ...