Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:
- You receive a valid board, made of only battleships or empty slots.
- Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape
1xN(1 row, N columns) orNx1(N rows, 1 column), where N can be of any size. - At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
Example:
X..X
...X
...X
In the above board there are 2 battleships.
Invalid Example:
...X
XXXX
...X
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
Follow up:
Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
public class Solution {
public int countBattleships(char[][] board) {
int m = board.length;
if (m == ) return ;
int n = board[].length, count = ;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (board[i][j] == '.') continue;
if (i > && board[i - ][j] == 'X') continue;
if (j > && board[i][j - ] == 'X') continue;
count++;
}
}
return count;
}
}
Battleships in a Board的更多相关文章
- [LeetCode] 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
https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...
- Leetcode: Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- 【LeetCode】419. Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- [Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...
- 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 ...
随机推荐
- 星云opencv总结
- php测试
Php基础知识测试题 姓名: 班级: 成绩: 本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A ) A:Windo ...
- 在 Sublime Text 3 中配置编译和运行 Java 程序
参考网址:http://www.open-open.com/lib/view/open1388105023765.html 1. 设置 java 的 PATH 环境变量 2. 创建批处理或 Shell ...
- caffe源码解析
http://blog.csdn.net/lanxuecc/article/details/53186613
- SSH(Struts2+Spring4+HIbernate5)的简化
今天给大家带来的是一个简单的新闻发布系统 首先在学习过程中我是深有体会,做事情不要浮躁,不要想着一口吃下一个胖子, 最最重要的是理解,理解透了学什么东西都是随心所欲的. 开发环境:win10系统 jd ...
- Android 轮换控件
首先是控件轮换 一.创建主布局 1.用到的控件是 TextSwitcher (文本轮换) 那么其他对应的也就是 ImageSwitcher (图片轮换) <LinearLayout xmlns: ...
- JavaScript replace() 方法
参考:http://www.w3school.com.cn/jsref/jsref_replace.asp 需要有一点注意的是:可以是函数的形式做为返回值,如下: "test{0}" ...
- bzoj4443[SCOI2015]小凸玩矩阵
题意:一个n*m的矩阵(n<=m<=250),要求选出n个数(每行,每列最多选一个),求第k大数的最小值. 首先第k大的意思是从大到小的第k个数(我读错了,WA了一次还以为算法不对...) ...
- [bigdata] 从Cloudera Manager中彻底删除服务器
1. 在CM中主机页里,选定要删除的服务器,选择操作“从群集中删除”. 2. 登录到主机,执行chkconfig,显示有cloudera-scm-agent服务, 执行service cloudera ...
- HTTP 基础知识
HTTP是一种协议.HTTP使用流程,一般情况下, 第一:由HTTP客户端发出请求,创建端口. 第二:HTTP服务器在端口监听客户端的请求. 第三:一旦收到请求,HTTP服务器向客户端返回状态和内容. ...