Leetcode: 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) or Nx1 (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?
只计算每个battleship的第一个元素,所以后面‘X’如果above或者left也是'X'的话,不被计算
public class Solution {
public int countBattleships(char[][] board) {
if (board==null || board.length==0 || board[0].length==0) return 0;
int count = 0;
for (int i=0; i<board.length; i++) {
for (int j=0; j<board[0].length; j++) {
if (board[i][j]=='X' && (i==0 || board[i-1][j]!='X') && (j==0 || board[i][j-1]!='X'))
count++;
}
}
return count;
}
}
Leetcode: 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 ...
- [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
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- 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
https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...
- 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 ...
随机推荐
- 使用jQuery 的.on() 提交表单
示例: $(function () { $(document).on('submit', '#FormId', function () { var val = $("#Name") ...
- BZOJ4552: [Tjoi2016&Heoi2016]排序
Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...
- - dequeueReusableCellWithIdentifier:
与之对应的还有一个方法: - dequeueReusableCellWithIdentifier:forIndexPath: 1 > - dequeueReusableCellWithIdent ...
- Odoo 9 Odoo $ JQuery undifned
浏览器处于假死状态,查看console发现 odoo,jquery,$ 未定义三处错误,后台显示IOError: IOError: [Errno 2] No such file or director ...
- LabVIEW如何将脚本插入Quick Drop
问题:如何将自己设计的LabVIEW脚本做成快捷键的方式,实现效果如下 解决: 第一步:在LabVIEW Data中新建Quick Drop Plugins 第二步 在文件夹下新建一个VI,VI接口的 ...
- html5用到的js
1.Zepto.js 是专门为现代智能手机浏览器退出的 Javascript 框架, 拥有和jQuery相似的语法, 但是和jQuery相比下来, 他有很多优点, 大小方面 , 压缩后的 zepto. ...
- 繁简体 互转 js
html: <script type="text/javascript" src="/js/s2t.js"></script><s ...
- DOS中cmd里常见的命令
我们使用计算机接触最频繁的就是DOS.DOS是英文Disk Operating System的缩写,意思是“磁盘操作系统”,顾名思义,DOS主要是一种面向磁盘的系统软件,说得简单些,DOS就是人给机器 ...
- VirtualMachine所支持的操作
在JDK中com.sun.tools.attach.VirtualMachine提供了一些从外部进程attach到jvm上,并执行一些操作的功能.VirtualMachine的子类HotSpotVir ...
- JavaBean组件的基本使用-语法
<jsp:useBean id="实例化对象名称" scope="保存范围" class="包.类名"> </jsp:us ...