[抄题]:

Given an 2D board, count how many 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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

i - 1 时请务必做好检查也要>=0,就是i >= 1

if (i >= 1 && board[i - 1][j] == 'X') continue;
if (j >= 1 && board[i][j - 1] == 'X') continue;

[思维问题]:

以为要用dfs,但是题目对所在的位置有特殊要求,就只能老老实实一个个地数了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

同一行或者同一列,就只数头不数尾

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

题目对所在的位置有特殊要求,就只能老老实实一个个地数了

[复杂度]:Time complexity: O(mn) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int countBattleships(char[][] board) {
//ini some variables
int m = board.length; int n = board[0].length;
int count = 0; //cc
if (board == null || m == 0 || n == 0) return 0; //for loop, only add once if qualified
for(int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == '.') continue;
if (i >= 1 && board[i - 1][j] == 'X') continue;
if (j >= 1 && board[i][j - 1] == 'X') continue; //add
count++;
}
} //return
return count;
}
}

419. Battleships in a Board 棋盘上的战舰数量的更多相关文章

  1. 419 Battleships in a Board 甲板上的战舰

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

  2. 419. Battleships in a Board

    https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...

  3. 【LeetCode】419. Battleships in a Board

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

  4. [LeetCode] 419. Battleships in a Board 平板上的战船

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

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

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

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

  7. [Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  8. [LeetCode] Battleships in a Board 平板上的战船

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

  9. Battleships in a Board

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

随机推荐

  1. Linq分组查询统计

    这里介绍Linq使用Group By和Count得到每个CategoryID中产品的数量,Linq使用Group By和Count得到每个CategoryID中断货产品的数量等方面. 学经常会遇到Li ...

  2. LG1337 [JSOI2004]平衡点 / 吊打XXX

    题意 题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不 ...

  3. linux下一些重要命令的了解

    linux下一些比较重要的命令: du命令: 查看使用空间: 格式: du [选项][文件] 参数: -a  显示目录中个别文件的大小. -b  显示目录或文件大小时,以byte为单位. -c  除了 ...

  4. Mac 永久添加 环境变量方法

    在 ~ 目录下 新建 .bash_profile 文件 在文件新增 export PATH="$PATH:/Users/zhangpengchao/tools/flutter/flutter ...

  5. [转]c++访问python3-实例化类的方法

    转自: http://blog.csdn.net/love_clc/article/details/76653100 此文是学习笔记,供日后翻阅.下面列出C++访问python所需的函数,按调用的先后 ...

  6. Ubuntu 14.10 下安装Ambari

    安装ambari有两种方式,一是自己下载源码编译,另外一个是使用公共仓库 1 使用Public Respositories Step1: Download the Ambari repository ...

  7. java中int算法的有趣现象

    今天无意中发现一个怪事,当时没理解,后来跟网友讨论了才知道原理,是关于int值的加法算法,两段代码如下: 代码1: @Test public void test1() { ; ; try { whil ...

  8. IIS 添加 MIME

    参考:https://blog.brain1981.com/727.html 在项目的  Web.Config 里下添加如下段落即可: <?xml version="1.0" ...

  9. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  10. [UE4]ProgressBar,进度条

    准备好2张进度条图片 一.新建名为“testProgress”的UserWidget,添加一个名为“ProgressBar_0”的ProgressBar到默认容器Canvas Panel 二.进度条进 ...