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 ...
随机推荐
- neo4j关闭和开启密码访问权限
本例:neo4j-enterprise-2.3.1版本 neo4j默认安装是开启访问密码验证 可以发现,在conf/下的neo4j-server.properties配置文件 # Require (o ...
- Django进阶(一)
Url进阶 mysit/mysit/urls.py from django.conf.urls import url from django.contrib import admin urlpatte ...
- 常用js归纳
一.获取地址栏参数 /*根据name获取URL参数*/ function getQueryString(name) { var reg = new RegExp("(^|&)&quo ...
- QPS 与 TPS 简介
QPS:Queries Per Second意思是"每秒查询率",是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS是Tra ...
- Angularjs -Promise - $http
https://www.peterbe.com/plog/promises-with-$http
- Scanner类
Scanner类:用于获取用户的键盘输入 成员方法: public boolean hasNextXxx():判断是否某种类型的元素 public Xxx nextXxx():获取该元素 常用方法: ...
- JS 循环给li绑定参数不同的点击事
以下内容纯属个人理解,不正确的地方还请大神留言,不胜感激! 源代码:(按个人方式选用一种即可) <ul> <li>1</li> <li>2</li ...
- 阿里云提示Discuz uc.key泄露导致代码注入漏洞uc.php的解决方法
适用所有用UC整合 阿里云提示漏洞: discuz中的/api/uc.php存在代码写入漏洞,导致黑客可写入恶意代码获取uckey,.......... 漏洞名称:Discuz uc.key泄露导致代 ...
- 将JAR包反编译,修改后重新打包(转)
将JAR包反编译,修改后重新打包(转) 在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的 ...
- 【jQuery】初始化的三种方法
JQ初始化方法实际上有两种,由于美元符号可以定义 jQuery,那么就有三种方法可以进行初始化操作,根据个人习惯来选择吧! 第一种 $(document).ready(function(){ // j ...