[抄题]:

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. Python垃圾回收详解:引用计数+标记清理+分代回收

    Python采用的是引用计数机制为主,标记-清理和分代收集两种机制为辅的策略. 1.引用计数 python中一切皆对象,所以python底层计数结构地就可以抽象为: 引用计数结构体{ 引用计数; 引用 ...

  2. 解决java.lang.IllegalArgumentException: No converter found for return value of type 的问题

    controller返回一个dto,并且定义了@ResponseBody注解,表示希望将这个dto对象转换为json字符串返回给前端,但是运行时报错:nested exception is java. ...

  3. ubuntu18关闭系统自动更新

    ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...

  4. Response的Content-Type一览

    文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .* application/octet-stream .tif image/t ...

  5. SQL 中 CASE - WHEN - THEN - ELSE - END 的小结

    在SQLServer中给变量赋备件值 可以用 @var = CASE WHEN EXPRESSION THEN STATEMENT01 ELSE STATEMENT02 END 当表达式 EXPRES ...

  6. is,as,类库

    is和as运算符: 所有类型的基类 object类型 - 基类:所有类型的基类,就类似是整个生物圈的生物类,是个大的概念 object o1 = new Random(); //object可以承载R ...

  7. 10K+,深度学习论文、代码最全汇总!

    我们大部分人是如何查询和搜集深度学习相关论文的?绝大多数情况是根据关键字在谷歌.百度搜索.想寻找相关论文的复现代码又会去 GitHub 上搜索关键词.浪费了很多时间不说,论文.代码通常也不够完整.怎么 ...

  8. git 在本地拉取远程分支的代码(并不做提交操作)

    1. git fetch 获取远程的所有分支 2. 在执行 git checkout -b local-branch-name origin/remote-branch  就可以将远程分支remote ...

  9. 表单:!!!常用JS: form 表单代码

    手机(文本框): <input type="text" name="" maxlength="11" placeholder=&quo ...

  10. python解决open()函数、xlrd.open_workbook()函数文件名包含中文,sheet名包含中文报错的问题

    问题现象: 1.使用open()函数.xlrd.open_workbook()函数打开文件,文件名若包含中文,会报错找不到这个文件或目录. 2.获取sheet时若包含中文,也会报错. #打开文件 fi ...