【LeetCode】419. Battleships in a Board
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) orNx1
(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的更多相关文章
- 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- centos6 安装python2.7+和神器pip
centos自带python版本是2.6.6,需要采用编译安装的方法安装py27 1.提前yum安装python以及pip神器依赖包(有没有必要都装上就是了,早晚有用): yum install -y ...
- openstack trove,使pylint忽略错误
一.什么是pylint Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除了 ...
- 研究表明,VR社交有助于内向者改变性格
目前,外界对于VR的普遍观点是这种头显提供了一种孤立的.反社会性的体验.但很少有人会意识到,虚拟现实同时也存在着能够增进人与人之间的关系的可能性. Facebook(脸书)IQ是人们连接这一社交网络的 ...
- linux 安装php的redis拓展
安装步骤: #wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz #tar -zxvf 2.2.4 #cd phpredi ...
- HDU 5868 Different Circle Permutation
公式,矩阵快速幂,欧拉函数,乘法逆元. $an{s_n} = \frac{1}{n}\sum\limits_{d|n} {\left[ {phi(\frac{n}{d})×\left( {fib(d ...
- Linq——Count、Sum、Min、Max、Average
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...
- nodejs 包引用的终极结论
通常我们用exports 或module.exports 来导出一个文件中的接口和字段,用require来引用导出的对象.那么这个exports 和 module.exports到底有啥关联呢? 1. ...
- C#下在图片文件本地
//C#下载图片文件到本地,c#,c#下载,下载图片,下载文件,下载函数// 从图片地址下载图片到本地磁盘// 将二进制文件保存到磁盘 using System;using System.Drawin ...
- PHP-FPM进程数的设定
近日,服务器出现异常,网站不能正常访问.经排查是php的问题. 在重启php-fpm时,恢复正常.1分钟之后又出现故障.查看php日志文件 /usr/local/php/var/log 后提示 WAR ...
- ElasticSearch(7)-排序
引用自ElaticSearch权威指南 一.排序 相关性排序 默认情况下,结果集会按照相关性进行排序 -- 相关性越高,排名越靠前. 这一章我们会讲述相关性是什么以及它是如何计算的. 在此之前,我们先 ...