419. Battleships in a Board 棋盘上的战舰数量
[抄题]:
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) 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩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 棋盘上的战舰数量的更多相关文章
- 419 Battleships in a Board 甲板上的战舰
给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句话 ...
- 419. Battleships in a Board
https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...
- 【LeetCode】419. Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- [LeetCode] 419. Battleships in a Board 平板上的战船
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...
- 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- [Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...
- [LeetCode] Battleships in a Board 平板上的战船
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
随机推荐
- IntelliJ IDEA 新版发布:支持CPU火焰图,新增酷炫主题
JetBrain 是一家伟大的公司,一直致力于为开发者开发世界上最好用的集成开发环境 就在上周,JetBrain 公司发布了 Java 集成开发环境 IntelliJ IDEA 最新版本 2018.3 ...
- @RequestParam接收解析不到 POST 提交的 数据
1.使用postman或者其他发送请求模拟器进行模拟访问,需要指定Headers为Content-Type:application/x-www-form-urlencoded;指定body类型为x-w ...
- mysql 将行拼接成字符串的方法
见代码: ;//保证可以拼接足够长的字符串,没它 数据量大时会截断结果1 group by videoType 效果如下:
- 【比赛打分展示双屏管理系统-专业版】Other.ini 配置文件解读以及排行榜界面及专家评语提交展示等具体配置
第一个问题:Other.ini配置文件的解读: 在软件根目录下,找到Other.ini配置文件,打开如下: 配置文件解读: iOrderIDOrXSID:默认为0,按照软件 选项/排行榜和奖项 的设置 ...
- mysql六种日志
错误日志 MySQL服务启动和关闭过程中的信息以及其它错误和警告信息.默认在数据目录下 普通查询日志 用于记录select查询语句的日志.general_log.general_log_file 默认 ...
- C#使用ITextSharp操作pdf
在.NET中没有很好操作pdf的类库,如果你需要对pdf进行编辑,加密,模板打印等等都可以选择使用ITextSharp来实现. 第一步:可以点击这里下载,新版本的插件升级和之前对比主要做了这几项重大改 ...
- ES6 入门Promise
Promise是一个对象用来传递异步操作的消息,有三种状态:Pending(进行中),Resolved(已完成又称Fulfilled)和Rejected(已失败). 特点:对象状态不受外界的影响.一旦 ...
- [Unity算法]点是否在多边形范围内
参考链接: https://www.zhihu.com/question/26551754 http://www.cnblogs.com/leoin2012/p/6425089.html 原理如下: ...
- js跨域传值,兼容ie8以上
js跨域传值,兼容ie8以上 事先说明,此方法并不支持ie8,如果想要支持ie8的话,需要用这种思路(来自微软): if (window.addEventListener) { window.addE ...
- SpringCloud系列三:SpringSecurity 安全访问(配置安全验证、服务消费端处理、无状态 Session 配置、定义公共安全配置程序类)
1.概念:SpringSecurity 安全访问 2.具体内容 所有的 Rest 服务最终都是暴露在公网上的,也就是说如果你的 Rest 服务属于一些你自己公司的私人业务,这样的结果会直接 导致你信息 ...