[抄题]:

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. enumerate()使用

    enumerate()使用 如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写: list1 = ["这", "是", "一个", ...

  2. linux环境下安装nginx步骤(不错)

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. ububtu平台编译环境可以使用以下指令 apt-get install build-essential apt-get install ...

  3. 深入理解CSS系列(一):理解CSS的盒子模型

    接触前端也有好几个年头了,但是,讲实话,对于CSS的理解真的是不敢恭维,相信很多同行也有类似的感受吧!这是为什么呢?因为我们都认为CSS太简单了,没有必要深入学习,果真如此?其实,只不过是自己图样图森 ...

  4. CMake for MFC example

    cmake_minimum_required(VERSION 2.8) add_definitions(-D_AFXDLL) set(CMAKE_MFC_FLAG 2) set(SRCS MFCApp ...

  5. golang垃圾回收和SetFinalizer

    golang自带内存回收机制--GC.GC通过独立的进程执行,它会搜索不再使用的变量,并释放.需要注意的是,进行GC会占用机器资源. GC是自动进行的.如果要手动进行GC,可以调用runtime.GC ...

  6. 关于java做题时需要注意的事项

    1.要熟悉eclipse的使用 2.用java提交时只能有一个public class 且类名只能为Main 3.提交时不能提交包名 4.提交时要将引入的包一起提交 5.虽然java提供了很多的函数, ...

  7. 【python】如何将ipdb的python解释器路径切换至虚拟环境中

    背景: 利用virtualenv构建一个python3.5的虚拟环境,在该虚拟环境中使用ipdb调试程序,结果报错找不到某一个模块. 程序的所有依赖模块都已经成功安装在虚拟环境中. 在虚拟环境中,te ...

  8. 学习笔记之Python爬虫

    Python 爬虫介绍 | 菜鸟教程 http://www.runoob.com/w3cnote/python-spider-intro.html https://blog.csdn.net/sina ...

  9. jsonp原理及同源策略

    [个人学习笔记,如有问题还请前辈纠正] jsonp 是用来跨域读取数据的,为什么从不同的域访问数据要用jsop呢?这源于一个著名的安全策略--同源策略,即: 协议.端口号.域名相同 举例说明:http ...

  10. Vue中table表头合并的用法

    <div class="panel-container"> <div> <table class="table-head" wid ...