[抄题]:

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. Linux系统安装管理

    将lfs linux liveCD的内容copy安装到硬盘 先将98.ima(dos启动软盘镜像文件)用ultraISO写入到u盘(usbhdd+), 不必勾选“创建启动分区”. 将liveCD和内核 ...

  2. oracle-taf

    http://blog.sina.com.cn/s/blog_48567d850102wck0.html配置目标:把RAC系统配置为“主-备”模式,即平时所有连接都在rac01这个节点上,当rac01 ...

  3. 第十一章 IO流

    11.IO流 11.1 java.io.File类的使用 1课时 11.2 IO原理及流的分类 1课时 11.3 节点流(或文件流) 1课时 11.4 缓冲流 1课时 11.5 转换流 1课时 11. ...

  4. py-day3 python 全局变量和局部变量

    # 全局变量 如果函数的内容无 global关键字,优先读取全局变量,无法对全局变量重新赋值, name = 'mj' def change_name(): print('change_name',n ...

  5. CSRF的防御解决过程

    CSRF是什么,就不多说,网络上的帖子多的去了,关于其定义. 这里主要介绍我们项目中,是如何解决这个问题的.方案比较简单,重点是介绍和记录一下遇到的问题和一些小的心得. 1. 解决方案 A. 用户登录 ...

  6. How to load custom styles at runtime (不会翻译,只有抄了 )

    原文 :http://blogs.embarcadero.com/sarinadupont/2013/10/16/how-to-load-custom-styles-at-runtime/ How t ...

  7. Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...

  8. Android手机上Audio DSP频率低 memory小的应对措施

    我在前面的文章(Android智能手机上的音频浅析)中说过Android手机上有一块专门用于音频处理的DSP,它的特点是频率低(一般几百MHZ).内部memory小(通常不超过100k word).要 ...

  9. 饥饿的牛(dp一维最大覆盖)

    问题 H: 饥饿的牛 时间限制: 1 Sec  内存限制: 128 MB提交: 12  解决: 12[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目 ...

  10. The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'

    --从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56 mysql> ...