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. 原生js实现fadein 和 fadeout

    js里面设置DOM节点透明度的函数属性:filter= "alpha(opacity=" + value+ ")"(兼容ie)和opacity=value/10 ...

  2. Web javascript 中常用API合集

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

  3. Ubuntu常用命令之update-alternatives

    maintain symbolic links determining default commands update-alternatives creates, removes, maintains ...

  4. 使用charles V3.11.2 实现SSL抓包

    首先,确认开启了SSL选项: 然后到Help->SSL Proxying里查看帮助; 根据提示,将android手机连接到局域网的wifi上,然后将wifi连接的代理设置为192.168.21. ...

  5. 把 SQL Server 迁移到 Linux?不如换成 MySQL

    最近几年,数量庞大的个人和组织放弃 Windows 平台选择 Linux 平台,而且随着人们体验到更多 Linux 的发展,这个数字将会继续增长.在很长的一段时间内, Linux 是网络服务器的领导者 ...

  6. ELKStack-使用消息队列扩展(十)

    ELKStack-使用消息队列扩展 官方文档:https://www.elastic.co/guide/en/logstash/5.x/deploying-and-scaling.html 流程图 流 ...

  7. 解决ubuntu16.04软件中心闪退的问题

    依次执行下列命令 sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install --reinstall software-cen ...

  8. 如何在MyEclipse上添加更换JRE

    如何在myeclipse上添加更换JRE 由于兼容性的问题,有些WEB项目会依赖jdk的版本.如果需要更换jdk,那么,知道如何更换JRE的方法很有必要. 一种在myeclipse上添加和更换JRE的 ...

  9. 换行的css属性

    //正常换行  word-break:keep-all;word-wrap:normal; //下面这行是自动换行  word-break:break-all;word-wrap:break-word ...

  10. ubuntu安装simplejson模块

    在terminal中输入 sudo apt-get install python-simplejson -y import simplejson print simplejson.dumps(lens ...