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. OData V4 系列 服务创建

    OData 学习目录 创建应用程序 添加引用 install-package entityframework . Install-Package Microsoft.AspNet.Odata . In ...

  2. 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况

    自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...

  3. iOS之隐藏键盘的方式

    一.//触摸空白处隐藏键盘 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_feedBackTextView r ...

  4. UIImageView

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. /***** ...

  5. python之面向对象与构造函数

    一.面向对象介绍 不同的编程范式本质上代表对各种类型的任务采取的不同的解决问题的思路, 大多数语言只支持一种编程范式, 当然也有些语言可以同时支持多种编程范式. 两种最重要的编程范式分别是面向过程编程 ...

  6. 在Asp.Net MVC中实现计算页面执行时间及简单流量统计

    引用www.rsion.com.dll进您的asp.net MVC项目本人不才,源代码中有详细说明,查看demo修改HomeController public class HomeController ...

  7. SQL SERVER导出特殊格式的平面文件

    有时候我们需要将SQL SERVER的数据一次性导入到ORACLE中,对于数据量大的表.我一般习惯先从SQL SERVER导出特殊格式的平面文件(CSV或TXT),然后用SQL*Loader装载数据到 ...

  8. Solr图形化界面banana:除Hue之外的选择

    最近Hue+Solr 方案原型验证有了一些进展.正好也收到了Google的大数据专家Sam的来件询问进展,我答复如下: Sam, 你好. 已经把Kafka+flume+solr的实时索引搭建起来了, ...

  9. 【转】Hive的insert操作

    insert 语法格式为: 1. 基本的插入语法: insert overwrite table tablename [partition(partcol1=val1,partclo2=val2)] ...

  10. C# 实现酒店房态图

    酒店管理系统最重要和实用的是能够及时.一目了然的反应房间状态的房态图,之前在开发一个的酒店管理系统中做了一个还算实用的房态图,现在分享下: 鼠标移到每个房间上面,可显示提示信息: 还可以自定义设置不同 ...