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 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"的更多相关文章
- [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
		
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大小的"船",注意船船之 ...
 - 419. Battleships in a Board 棋盘上的战舰数量
		
[抄题]: Given an 2D board, count how many battleships are in it. The battleships are represented with ...
 - 419 Battleships in a Board 甲板上的战舰
		
给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句话 ...
 - [LeetCode] Battleships in a Board 平板上的战船
		
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
 - Leetcode: Battleships in a Board
		
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
 - leetcode 419
		
题目说明: Given an 2D board, count how many different battleships are in it. The battleships are represe ...
 
随机推荐
- GJM : C#语言学习笔记
			
--------------------------------------C#--------------------------------------if (tom == null) tom = ...
 - ASP.NET MVC+EF框架+EasyUI实现权限管理系列(22)-为用户设置角色
			
ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo (3):面向接口编程 (4 ):业务逻辑层的封装 ...
 - Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
			
前言 啦啦啦~大家好,又见面啦~ 本篇博文讲和大家一起完成一个需要注册.登录的备忘录的,一起学习 SharedPreferences 的基本使用,学习 Android 中常见的文件操作方法,复习 An ...
 - IOS开发基础知识--碎片18
			
1:initWithFrame方法的理解 . initWithFrame方法是什么? initWithFrame方法用来初始化并返回一个新的视图对象,根据指定的CGRect(尺寸). 当然,其他UI对 ...
 - iPhone被盗后续更新二:被换机!已取机!没扣住新机!怎么找新机呢?事发半年后跟进...
			
先说下情况 MEID/IMEI:3544 2706 9380 456 我的序列号:F17NL088G5MY 新的IMEI:3569 7606 5956 097 新的序列号:DNPNV69ZG5MY 我 ...
 - 【转】hive导入数据出现NULL
			
在把hdfs上数据迁移到hive中的表时,若出现数据位NULL,是因为没有指定列分隔符. 由于hive默认的分隔符是/u0001(Ctrl+A),为了平滑迁移,需要在创建表格时指定数据的分割符号,语法 ...
 - MongoDBV3.0.7版本(shard+replica)集群的搭建及验证
			
集群的模块介绍: 从MongoDB官方给的集群架构了解,整个集群主要有4个模块:Config Server.mongs. shard.replica set: Config Server:用来存放集群 ...
 - Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理8
			
接下来做的是对页面的增删改查与页面与页面按钮之间的联系.先上代码和页面效果 using AuthorDesign.Web.App_Start.Common; using System; using S ...
 - Mybatis整合Spring
			
根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...
 - Jenkins部署到远程(Linux服务器)
			
接着上次的说,上次只是实现了本地自动化部署,这种情况只是针对开发环境和部署环境在同一台机器时适用.不过,一般情况下,我们都会要把项目部署到远程Linux服务器上,所以这节的主要内容是: 1.部署开发环 ...