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?

 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的更多相关文章

  1. [LeetCode] 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

    https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...

  3. Leetcode: Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  4. 【LeetCode】419. Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  5. [Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  6. 419. Battleships in a Board 棋盘上的战舰数量

    [抄题]: Given an 2D board, count how many battleships are in it. The battleships are represented with  ...

  7. [LeetCode] 419. Battleships in a Board 平板上的战船

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  8. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 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 ...

随机推荐

  1. hibernate通过注解实现实体和表的映射

    参考: 表名的映射: //代表此类参与ORM映射,此注解必须要有 @Entity //代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略 @Table(name=" ...

  2. Web javascript 中常用API合集

    来源于:https://www.kancloud.cn/dennis/tgjavascript/241852 一.节点 1.1 节点属性 Node.nodeName //返回节点名称,只读 Node. ...

  3. 移动端重要的几个CSS3属性设置

    去掉点击链接和文本框对象的半透明覆盖(iOS)或者虚框(Android) -webkit-tap-hightlight-color:rgba(0,0,0,0); 禁用长按页面时弹出菜单(iOS下有效) ...

  4. UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)

    现在都知道,在初始化UICollectionView的时候,必须要传入一Layout对象,进行布局管理.这也是collectionview和tableview的明显区别,通过collectionvie ...

  5. ITIL十大流程

    1.服务水平管理(Service Level Management):服务水平管理的目标是通过协调IT用户和提供者双方的观点,实现特定的.一致的.可测量的服务水平,以为客户节省成本.提高用户生产率. ...

  6. c# Using Settings under visual studio 2012

    1.在项目属性中的Settings的设置可以通过以下方式调用 Properties.Settings.Default.(SpecifyPropertyName) 2.添加新的Settings设置文件 ...

  7. HiShop2.x版本中的上传插件分析,得出所用的模板语言为Underscore.js 1.6.0且自己已修改

    效果: 上传组件非常的酷,但是分析其使用JS写法使用了模板语言的,代码如下: <script type="text/j-template" id="tpl_popb ...

  8. CSS中清除浮动的两种方式

    在CSS中,父元素中的子元素如果使用了float,会导致父元素塌陷,高度为0. 对于这种情况,常见的解决方式有两种. 一.增加新的div,应用clear:both属性 html: <div cl ...

  9. 偷懒小工具 - SSO单点登录通用类(可跨域)

    写在前面的话 上次发布过一篇同样标题的文章.但是因为跨域方面做得不太理想.我进行了修改,并重新分享给大家. 如果这篇文章对您有所帮助,请您点击一下推荐.以便有动力分享出更多的"偷懒小工具&q ...

  10. dynamic 用法

    private static string GetNameValue(object value) { dynamic obj = value; try { return obj.Name; } cat ...