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 ...
随机推荐
- js整理
Js脚本语音 网页里面使用的脚本语音 基础语法 注释语法 单行注释// 多行注释/**/ 嵌入js代码 尽量靠下写 用<script type="text/javascript& ...
- Linux下 JDK安装
在linux下安装JDK步骤如下: 第一步:查看Linux自带的JDK是否已安装 (1)查看jdk: [root@web-server ~]# rpm -qa|grep jdk ← 查看jdk的信息或 ...
- C#检验参数合法性公用方法
#region 检验参数合法性,数值类型不小于0,引用类型不能为null,否则抛出异常 /// <summary> /// 检验参数合法性,数值类型不小于0,引用类型不能为null,否则抛 ...
- Android 横竖屏+碎片的应用
最终效果展示: 项目介绍: 通过碎片的方式显示标题列表和内容,其中也牵涉到横竖屏的知识 项目代码下载:http://files.cnblogs.com/files/Laopengblog/%E7%A2 ...
- Java 配置maven及阿里云镜像
一:配置maven 1.下载maven,选择Binary tar.gz,解压拷贝到目录/usr/local/ https://maven.apache.org/download.cgi 2.配置系统默 ...
- Android中利用AIDL机制调用远程服务
服务端: //CalculateInterface.aidl package com.itheima.aidl.calculate; interface CalculateInterface { do ...
- 转:sublime上使用git连接github
"工欲善其事,必先利其器." 这是古人的教诲,也是一个高效率的工程师需要遵循的法则之一.从大学开始写Java使用了JBuilder,Eclipse,后来写PHP用了Zend,写Ja ...
- webpack使用优化(基本篇)
转自:https://github.com/lcxfs1991/blog/issues/2 前言 本文不是webpack入门文章,如果对webpack还不了解,请前往题叶的Webpack入门,或者阮老 ...
- JAVA文档注释标签
1 常用Java注释标签(Java comment tags) @author 作者 @param 输入参数的名称 说明 @return 输出参数说明 @since JDK版本 @version ...
- Redis实战阅读笔记——开始
Redis实战这本书,看完以后最大的不是redis本身的东西,而是作者面对实际问题的分析而给出的设计方案,可以看成NoSql设计的应用.个人从这方面收获很多,至于Redis本身的东西,这个就花一两个小 ...