The follow-up question is fun: "Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?"

When we meet an 'X', we need to check if it is vertical or horizontal: they will never happen at the same time, by problem statement. For horizontal, we simply stripe through right, and plus 1 - however, if our top element is 'X' already, it is a vertical and counter has alread been increased.

class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int h = board.size();
if (!h) return ;
int w = board[].size();
if (!w) return ; int cnt = ;
for(int i = ; i < h; i ++)
for(int j = ; j < w; j ++)
{
if(board[i][j] == 'X')
{
// is it a counted vertical case?
if(!(i > && board[i -][j] == 'X'))
{
cnt ++;
// Horizontal
while(j < (w - ) && board[i][j + ] == 'X') j ++;
}
}
} return cnt;
}
};

LeetCode "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. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)

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

  3. 【LeetCode】419. Battleships in a Board

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

  4. 419. Battleships in a Board

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

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

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

  6. 419 Battleships in a Board 甲板上的战舰

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

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

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

  8. Leetcode: Battleships in a Board

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

  9. leetcode 419

    题目说明: Given an 2D board, count how many different battleships are in it. The battleships are represe ...

随机推荐

  1. SQL Server 索引

    SQL Server 中数据存储的基本单位是页(Page).数据库中的数据文件(.mdf 或 .ndf)分配的磁盘空间可以从逻辑上划分成页(从 0 到 n 连续编号).磁盘 I/O 操作在页级执行.也 ...

  2. JavaEE中Web服务器、Web容器、Application服务器区别及联系

    在JavaEE 开发Web中,我们经常会听到Web服务器(Web Server).Web容器(Web Container).应用服务器(Application Server),等容易混淆不好理解名词. ...

  3. Bootstrap组件之响应式导航条

    响应式导航条:在PC和平板中默认要显示所有的内容:但在手机中导航条中默认只显示“LOGO/Brand”,以及一个“菜单折叠展开按钮”,只有单击折叠按钮后才显示所有的菜单项. 基础class: .nav ...

  4. arcgis软件集合

    ArcGIS软件应有尽有,跨越各种不同的ArcGIS版本,包括ArcGIS10.3.ArcGIS10.2.2.ArcGIS10.1.ArcGIS10.0.ArcGIS9.3. 备注:更多的精彩内容请关 ...

  5. 基本排序算法——冒泡排序java实现

    冒泡排序是原理最简单的一种排序算法,具体思想就不多说了,代码如下: eclipse4.3中编译通过 package sort.basic; import java.util.Arrays; publi ...

  6. SharePoint 2013 Search 配置总结

    前言:SharePoint 2013集成了Fast搜索以后,搜索的配置有了些许改变,自己在配置过程中,也记录下了一些入门的东西,希望能够对大家有所帮助. 1.配置搜索服务,首先需要启用搜索功能,基本思 ...

  7. View and Data API 现在支持IE11了

    By Daniel Du After a long time waiting, IE11 finally supports WebGL, which enables us viewing our 3D ...

  8. # iOS 10 适配 # 适配刷新控件 以MJRefresh 为例

    在iOS10中ScrollView 添加了一个refreshControl的东西 - - 不知道水果公司做了什么  导致原有的刷新控件类刷新后frame.y 向下偏移了20 起初以为是水果调整了sta ...

  9. Expdp 导数错误 ORA-00832

    问题实验环境 操作系统:Red Hat Enterprise Linux Server release 5.7 (Tikanga) 数据库  :Oracle Database 10g Release ...

  10. Provider:SSL Provider,error:0-等待的操作过时

    今天一同事使用SSMS 2012 连接数据库时,遇到了"provider:SSL Provider,error:0-等待的操作过时",搜索了一下,遇到一哥 们也遇到这个问题:SQL ...