leetcode 419
题目说明:
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.
解法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的更多相关文章
- [LeetCode] 419. Battleships in a Board 平板上的战船
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...
- Java实现 LeetCode 419 甲板上的战舰
419. 甲板上的战舰 给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直 ...
- 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 ...
- Leetcode 419.甲板上的战舰
甲板上的战舰 给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句 ...
- 【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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 自定义Toast和RatingBar的简单用例
Toast是一个包含用户点击消息.Toast类会帮助你创建和显示这些.Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动 向右划动五角星增加 单击按钮显示自 ...
- Channel
提起Channel,JDK的NIO类库的重要组成部分,就是提供了java.nio.SocketChannel和java.nio.ServerSocketChannel,用于非阻塞的I/O操作. 类似于 ...
- 《DSP using MATLAB》示例Example5.15
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y1 = circonvt(x1,x2,5); % N = 5 n1 = 0:1:length(x1)-1; n2 = 0:1:le ...
- PHP 汉字数字互转(100以内)| PHP 汉字转数字 | PHP数字转汉字
<?php function numDatabase(){ $numarr =array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2 ...
- sql 动态行转列
create table u01 (医案编号 varchar(5),药物编号 varchar(5)) insert into u01 select '01','01' union all select ...
- 2016 Multi-University Training Contest 1
8/11 2016 Multi-University Training Contest 1 官方题解 老年选手历险记 最小生成树+线性期望 A Abandoned country(BH) 题意: 1. ...
- android studio每次启动都要在fetching Android sdk compoment information停好久的解决方案
1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开.2)在idea.properties文件末尾添加一行: disable.and ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 关于Openlayer3的菜鸟认识
什么是OpenLayers? OpenLayers 是一个专为Web GIS 客户端开发提供的JavaScript 类库包,用于实现标准格式发布的地图数据访问.从OpenLayers2.2版本以后,O ...
- ibatis #于 $区别
系统框架用ibatis,开发中ibatis配置文件中执行order by #orderByClause# ,怎么搞都没有效果, 后面改成 order by $orderByClause$,OK,问题解 ...