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. OLTP(on-line transaction processing)与OLAP(On-Line Analytical Processing)

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  2. C# 动态修改Config

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); confi ...

  3. FragmentTabHost简单保存状态的方法

    private View rootView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container ...

  4. css3-无缝滚动

    @keyframes 规则用于创建动画.在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果. 动画的名称和运行所需时间是必须的 帧动画:将动画名称赋给选择器 ...

  5. cocos2d-x 3.5以后版本的 luasocket

    cocos2d-x 3.5后使用luasocket:local SOCKET = require "socket"; 结果运行就报错:[LUA-print] USE " ...

  6. 思维导图FreeMind安装问题及简单使用

    思维导图软件使用的坎坷之路 一直想将思维导图加入到工作环境当中 最开始使用的是 MindManager(http://www.mindmanager.cc/) ,而且感觉利用它制作出来的导图外观也比较 ...

  7. Newick format tree

    1. all branches + leaf names + internal supports ((D:0.723274,F:0.567784)1.000000:0.067192,(B:0.2793 ...

  8. JAVA实现带图片的列表——JList

    JList:显示对象列表并且允许用户选择一个或多个项的组件. JList的构造方法: 1.根据数组创建列表: JList(Object[] listData) 构造一个 JList,使其显示指定数组中 ...

  9. saltstack初探

    salt-key -y -d linux-node1 #删除linux-node1节点的认证 salt -G 'cpuarch:x86_64' grains.item num_cpus >> ...

  10. EasyPusher应用

    转自https://github.com/EasyDarwin/EasyPusher 本文仅实际体验一下demo,分析一下如何应用. 1)EasyPusher框图预览 2) EasyPusher应用实 ...