给定一个二维的甲板, 请计算其中有多少艘战舰。 战舰用 'X'表示,空位用 '.'表示。 你需要遵守以下规则:
    给你一个有效的甲板,仅由战舰或者空位组成。
    战舰只能水平或者垂直放置。换句话说,战舰只能由 1xN (1 行, N 列)组成,或者 Nx1 (N 行, 1 列)组成,其中N可以是任意大小。
    两艘战舰之间至少有一个水平或垂直的空位分隔 - 即没有相邻的战舰。
示例 :
X..X
...X
...X
在上面的甲板中有2艘战舰。
无效样例 :
...X
XXXX
...X
你不会收到这样的无效甲板 - 因为战舰之间至少会有一个空位将它们分开。
进阶:
你可以用一次扫描算法,只使用O(1)额外空间,并且不修改甲板的值来解决这个问题吗?
详见:https://leetcode.com/problems/battleships-in-a-board/description/

C++:

class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
if (board.empty())
{
return 0;
}
int rsize = board.size();
int csize = board[0].size();
int count = 0;
for (int i = 0; i < rsize; i++)
{
for (int j = 0; j < csize; j++)
{ if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.'))
{
count++;
}
}
}
return count;
}
};

参考:https://blog.csdn.net/camellhf/article/details/52871104

https://www.cnblogs.com/grandyang/p/5979207.html

419 Battleships in a Board 甲板上的战舰的更多相关文章

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

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

  2. Java实现 LeetCode 419 甲板上的战舰

    419. 甲板上的战舰 给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直 ...

  3. Leetcode 419.甲板上的战舰

    甲板上的战舰 给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句 ...

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

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

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

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

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

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

  7. 419. Battleships in a Board

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

  8. 【LeetCode】419. Battleships in a Board

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

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

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

随机推荐

  1. 区分Integer.getInteger和Integer.valueOf、Integer.parseInt() 的使用方法

    Integer类有两个看起来很类似的静态方法,一个是Integer.getInteger(String),另外一个是Integer.valueOf(String).如果只看方法名称的话,很容易将这两个 ...

  2. jquery全选,取消全选

    近期项目又用到了这个全选和取消全选的操作. 曾经总是自己写纯JS.如今既然知道怎么写了.那怎样用JQ写得更简洁呢.这样也能学到新的东西.假设乎百度一下果然发现了好东东.感谢OSC的iuhoay. 代码 ...

  3. mac for smartSVN9 (8,9)破解方法 附smartSvn_keygen工具图文

    mac for  smartSVN9 (8,9)破解方法 附smartSvn_keygen工具 工具文件下载: http://files.cnblogs.com/files/xueshanshan/s ...

  4. VS创建Web项目提示配置IISExpress失败

    开发服务器VS2013,新建Web项目提示: 打开Web项目提示: 解决方法:控制面板,找到IISExpress,右键 选择修复,解决问题..

  5. Android调用本地WebService

    package com.example.testinvokewebservice; import org.ksoap2.SoapEnvelope; import org.ksoap2.serializ ...

  6. Hibernate 之 Locking

    在我们业务实现的过程中,往往会有这样的需求:保证数据访问的排他性,也就是我正在访问的数据,别人不能够访问,或者不能对我的数据进行操作.面对这样的需求,就需要通过一种机制来保证这些数据在一定的操作过程中 ...

  7. FunsionCharts Demo

    原文路径:http://www.cnblogs.com/xuhongfei/archive/2013/04/12/3016882.html 一.简介 Ø FusionCharts 是InfoSoft  ...

  8. Entity FramWork Code first 使用心得

    1 最有用的命令 update-database -force -verbose 2 主键如果不是默认的int或者 bigint而是guid 或者 string类型,创建记录的时候要给主键赋值 3 在 ...

  9. Magic Grid ComboBox JQuery 版

    在MagicCombo组件中嵌入Grid,以支持分页查找和跨页选取 ​ 1. ​2. [代码][JavaScript]单选示例代码     <script type="text/jav ...

  10. hdu3555(数位DP dfs/递推)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...