Skip to content

Commit 54a2e7f

Browse files
authored
Merge pull request #423 from LetMeFly666/419
添加问题“419.甲板上的战舰”的代码和题解
2 parents 326dad2 + c5c398c commit 54a2e7f

6 files changed

+205
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-11 10:15:35
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-11 12:19:59
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countBattleships(vector<vector<char>>& board) {
14+
int ans = 0;
15+
for (int i = 0; i < board.size(); i++) {
16+
for (int j = 0; j < board[0].size(); j++) {
17+
if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) {
18+
ans++;
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-11 12:24:27
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-11 12:26:00
6+
*/
7+
package main
8+
9+
func countBattleships(board [][]byte) int {
10+
ans := 0
11+
for i := 0; i < len(board); i++ {
12+
for j := 0; j < len(board[0]); j++ {
13+
if board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.') {
14+
ans++
15+
}
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-11 12:22:38
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-11 12:24:00
6+
*/
7+
class Solution {
8+
public int countBattleships(char[][] board) {
9+
int ans = 0;
10+
for (int i = 0; i < board.length; i++) {
11+
for (int j = 0; j < board[0].length; j++) {
12+
if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) {
13+
ans++;
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-11 12:20:29
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-11 12:21:50
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countBattleships(self, board: List[List[str]]) -> int:
11+
return sum(board[i][j] == 'X' and (i == 0 or board[i - 1][j] == '.') and (j == 0 or board[i][j - 1] == '.') for j in range(len(board[0])) for i in range(len(board)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
|0409.最长回文串|简单|<a href="https://leetcode.cn/problems/longest-palindrome/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/17/LeetCode%200409.%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127359550" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-palindrome/solution/letmefly-409zui-chang-hui-wen-chuan-by-t-xsid/" target="_blank">LeetCode题解</a>|
175175
|0410.分割数组的最大值|困难|<a href="https://leetcode.cn/problems/split-array-largest-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/21/LeetCode%200410.%E5%88%86%E5%89%B2%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135728821" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-array-largest-sum/solutions/2613456/letmefly-410fen-ge-shu-zu-de-zui-da-zhi-agh2v/" target="_blank">LeetCode题解</a>|
176176
|0415.字符串相加|简单|<a href="https://leetcode.cn/problems/add-strings/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/17/LeetCode%200415.%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%8A%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131758488" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/add-strings/solutions/2346886/letmefly-415zi-fu-chuan-xiang-jia-mo-ni-r1ph3/" target="_blank">LeetCode题解</a>|
177+
|0419.甲板上的战舰|中等|<a href="https://leetcode.cn/problems/battleships-in-a-board/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/11/LeetCode%200419.%E7%94%B2%E6%9D%BF%E4%B8%8A%E7%9A%84%E6%88%98%E8%88%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139595909" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/battleships-in-a-board/solutions/2807404/letmefly-419jia-ban-shang-de-zhan-jian-t-2bhw/" target="_blank">LeetCode题解</a>|
177178
|0421.数组中两个数的最大异或值|中等|<a href="https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/19/LeetCode%200421.%E6%95%B0%E7%BB%84%E4%B8%AD%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%82%E6%88%96%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127413219" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/solution/letmefly-421shu-zu-zhong-liang-ge-shu-de-zaxe/" target="_blank">LeetCode题解</a>|
178179
|0429.N叉树的层序遍历|中等|<a href="https://leetcode.cn/problems/n-ary-tree-level-order-traversal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/17/LeetCode%200429.N%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136136336" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/n-ary-tree-level-order-traversal/solutions/2642557/letmefly-429n-cha-shu-de-ceng-xu-bian-li-k66j/" target="_blank">LeetCode题解</a>|
179180
|0445.两数相加II|中等|<a href="https://leetcode.cn/problems/add-two-numbers-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/14/LeetCode%200445.%E4%B8%A4%E6%95%B0%E7%9B%B8%E5%8A%A0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127316269" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/add-two-numbers-ii/solution/letmefly-445liang-shu-xiang-jia-ii-by-ti-vbh5/" target="_blank">LeetCode题解</a>|
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: 419.甲板上的战舰
3+
date: 2024-06-11 12:27:23
4+
tags: [题解, LeetCode, 中等, 深度优先搜索, 数组, 矩阵]
5+
---
6+
7+
# 【LetMeFly】419.甲板上的战舰:统计战舰头(左上角)——附Py一行版
8+
9+
力扣题目链接:[https://leetcode.cn/problems/battleships-in-a-board/](https://leetcode.cn/problems/battleships-in-a-board/)
10+
11+
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示甲板,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在甲板 <code>board</code> 上放置的 <strong>战舰</strong> 的数量。</p>
12+
13+
<p><strong>战舰</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,战舰只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状建造,其中 <code>k</code> 可以是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/battelship-grid.jpg" style="width: 333px; height: 333px;" />
19+
<pre>
20+
<strong>输入:</strong>board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
21+
<strong>输出:</strong>2
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>board = [["."]]
28+
<strong>输出:</strong>0
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong>提示:</strong></p>
34+
35+
<ul>
36+
<li><code>m == board.length</code></li>
37+
<li><code>n == board[i].length</code></li>
38+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
39+
<li><code>board[i][j]</code> 是 <code>'.'</code> 或 <code>'X'</code></li>
40+
</ul>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>进阶:</strong>你可以实现一次扫描算法,并只使用<strong> </strong><code>O(1)</code><strong> </strong>额外空间,并且不修改 <code>board</code> 的值来解决这个问题吗?</p>
45+
46+
47+
48+
## 解题方法:统计战舰左上角
49+
50+
这题的意思是,若出现战舰```X```,则战舰```X```的形状必定是“一行或一列连着几个”。
51+
52+
因此我们直接统计有多少个“战舰头”不就可以了吗。
53+
54+
遍历每个方格,若当前方格为战舰```X```,则满足以下条件时该方格为“战舰头”:
55+
56+
> + 该方格左边是边界或空地
57+
> + 该方格上边是边界或空地
58+
59+
+ 时间复杂度$O(size(board))$
60+
+ 空间复杂度$O(1)$
61+
62+
### AC代码
63+
64+
#### C++
65+
66+
```cpp
67+
class Solution {
68+
public:
69+
int countBattleships(vector<vector<char>>& board) {
70+
int ans = 0;
71+
for (int i = 0; i < board.size(); i++) {
72+
for (int j = 0; j < board[0].size(); j++) {
73+
if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) {
74+
ans++;
75+
}
76+
}
77+
}
78+
return ans;
79+
}
80+
};
81+
```
82+
83+
#### Go
84+
85+
```go
86+
// package main
87+
88+
func countBattleships(board [][]byte) int {
89+
ans := 0
90+
for i := 0; i < len(board); i++ {
91+
for j := 0; j < len(board[0]); j++ {
92+
if board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.') {
93+
ans++
94+
}
95+
}
96+
}
97+
return ans
98+
}
99+
```
100+
101+
#### Java
102+
103+
```java
104+
class Solution {
105+
public int countBattleships(char[][] board) {
106+
int ans = 0;
107+
for (int i = 0; i < board.length; i++) {
108+
for (int j = 0; j < board[0].length; j++) {
109+
if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) {
110+
ans++;
111+
}
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
#### Python
120+
121+
```python
122+
# from typing import List
123+
124+
class Solution:
125+
def countBattleships(self, board: List[List[str]]) -> int:
126+
return sum(board[i][j] == 'X' and (i == 0 or board[i - 1][j] == '.') and (j == 0 or board[i][j - 1] == '.') for j in range(len(board[0])) for i in range(len(board)))
127+
```
128+
129+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/11/LeetCode%200419.%E7%94%B2%E6%9D%BF%E4%B8%8A%E7%9A%84%E6%88%98%E8%88%B0/)哦~
130+
>
131+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139595909](https://letmefly.blog.csdn.net/article/details/139595909)

0 commit comments

Comments
 (0)