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.

找到有多少条船,横向或纵向不相隔的X组成一条船,船之间至少有一个空点

解题思路:

遍历二维数组,每遍历到一个位置的时候,只需要考虑其左边和上面的位置是否为X即可

 int countBattleships(char** board, int boardRowSize, int boardColSize) {
int i,j;
int count=;
for(i=;i<boardRowSize;i++)
for(j=;j<boardColSize;j++)
if(board[i][j]=='X')
{
if(i==)
{
if(j==||board[i][j-]=='.')
count++;
}
else
{
if((j==&&board[i-][j]=='.')||(board[i-][j]=='.'&&board[i][j-]=='.'))
count++;
}
}
return count;
}
//遍历到每个元素时候,只需要考虑其前面和上面是否为'.'即可

【LeetCode】419. Battleships in a Board的更多相关文章

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

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

  2. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  3. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. openstack trove,使pylint忽略错误

    一.什么是pylint Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除了 ...

  2. 转载--初识绘图工具plantUML

    原文出处:http://www.jianshu.com/p/e92a52770832 什么是 PlantUML PlantUML 是一个画图脚本语言,用它可以快速地画出: 时序图 流程图 用例图 状态 ...

  3. Jmeter之基本介绍

    初学Jmeter,以下是我常用的功能,总结一下. 1.Thread Group线程组 线程组,即:虚拟用户组

  4. 将Cygwin Emacs设为Windows explorer默认打开程序

    由于我在平日的学习与工作中会经常用到Cygwin中的Emacs,很自然地想到应该将emacsclient作为指定文件类型在Windows explorer中的默认打开程序.这样,便可以直接双击文件后在 ...

  5. 面向对象---java代码块

    概念:代码块是指用{}括起来的一段代码. 根据位置及声明的关键字不同,代码块可分为普通代码块.构造块.静态代码块.同步代码块4种. 1.普通代码块: 直接在方法中或在语句中定义 public clas ...

  6. Tiny6410之重定位代码到SRAM+4096

    重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...

  7. 解决Intellij Idea里tomcat启动报The JRE_HOME environment variable is not defined correctly的错误

    创建一个maven项目后想启动tomcat测试一下新建的maven项目,结果一直报The JRE_HOME environment variable is not defined correctly, ...

  8. 使用curl来调试你的应用

    我们在客户端开发过程中总免不了和后端进行api对接,有时候需要对返回的数据格式进行调试,有时候每次运行客户端来发送请求,这个未免效率太低,这里就来介绍一个好用的工具--curl. curl curl是 ...

  9. Gs_Class._BaseQueryWeb查询页面基类(aspx.net)

    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...

  10. CSS行高line-height的一些深入理解及应用

    一.一些字面意思. “行高”大约是指:一行文字的高度.具体来说是指两行文字间基线之间的距离.基线是在英文字母中用到的一个概念,我们刚学英语使用的那个英语本子每行有四条线,其中底部第二条线就是基线,是a ...