题目说明:

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) or Nx1 (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.

解法1思路:

这里需要计算战舰队的数量count,可转换为计算每个战舰队位于最左上角的船只(我们称为战舰队头)。 用两个循环,对网格内的每一个格子进行遍历,若该格子为X(即有战舰),则分以下几种情况: 
若在网格的最左上角(即i=0,j=0),则存在一个舰队,count自加1; 
若在网格的最上角(即i=0),但不在最左边(即j!=0),则需要判断该位置的左边(即board[i][j-1])是否有战舰存在,若其左边不存在战舰,则该位置为战舰队头,count自加1; 
若在网格的最左边(即j=0),但不在最上边(即i!=0),则需要判断该位置的上面(即board[i-1][j])是否有战舰存在,若其上面不存在战舰,则该位置为战舰队头,count自加1; 
若战舰不在最左边也不在最上边(即i!=0且j!=0),则需要判断该战舰的左边和上面是否还有战舰存在,若其左边和上面都不存在战舰,则该位置为战舰队头,count自加1; 
其余情况,战舰都不属于战舰队头。

解法1代码:

int countBattleships(vector<vector<char>>& board)
{
int counter = ;
for (int i = ; i < board.size(); i ++) {
for (int j = ; j < board[i].size(); j ++) {
if (board[i][j] == 'X') {
if (i == && j == )
counter ++;
else if (i == && j != && board[i][j - ] == '.')
counter ++;
else if (j == && i != && board[i - ][j] == '.')
counter ++;
else if (i != && j != && board[i - ][j] == '.' && board[i][j - ] == '.')
counter ++;
} }
} return counter;
}

解法2思路:

和解法1相似,只是这种解法是从反面分析,逐项排除非battleships的项,最后留下符合条件的项,这种剪枝策励在leetcode的算法题中非常常见。

解法2代码:

int countBattleships(vector<vector<char>>& board)
{
int counter = ;
for (int i = ; i < board.size(); i ++) {
for (int j = ; j < board[i].size(); j ++) {
if (board[i][j] == '.')
continue;
if (j > && board[i][j - ] == 'X')
continue;
if (i > && board[i - ][j] == 'X')
continue; ++ counter;
}
} return counter;
}

部分引用自:

http://blog.5ibc.net/p/94878.html

http://blog.csdn.net/mebiuw/article/details/52876700

leetcode 419的更多相关文章

  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. 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 ...

  4. Leetcode 419.甲板上的战舰

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

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

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

  6. 【LeetCode】419. Battleships in a Board

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

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 【转】c# Image获得图片路径的三种方法 winform

    代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...

  2. Android开发各类常见错误解决方案

    本文属于个人平时项目开发过程遇到的一些问题,记录下来并总结解决方案,希望能帮到大家解决问题,有些问题的解决方案是在StackoverFlow上找到的,建议大家遇到问题多去上面找,基本上都能找到解决方案 ...

  3. psql-09表:视图和索引

    视图 由查询语句定义的虚拟表;从视图中看到的数据可能来自数据库中的一张或多张表,也可能来自外部; 使用视图的原因一般有: 使复制的查询易于理解和使用; 安全原因; 表一些函数返回的结果映射成视图; 一 ...

  4. C#-ASP.NET MVC-架构【1】-自定义错误页

    自定义异常本来是一件很简单的事情,没想到在做的过程中遇到各种坑,目前来说,还有Session过期和Ajax请求这两种情况没有特殊处理,其他的基本已经可以使用,等慢慢完善吧. 一.在web.config ...

  5. C++: virtual inheritance and Cross Delegation

    Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...

  6. Spring任务调度之Quartz

    一.Quartz作业类的继承方式来讲,可以分为两类: 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.Quart ...

  7. java实现a+b的多重输入

    import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner cin ...

  8. NOI 题库 7624

    7624  山区建小学 描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中, ...

  9. 【ORACLE】特殊的NULL

    NULL 是数据库中特有的数据类型 Oracle 中对空的描述 nullAbsence of a value in a column of a row. Nulls indicate missing, ...

  10. socket是什么?(翻译)

    根据stackoverflow的答案: 原文:A socket represents a single connection between two network applications. The ...