Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0faeb29

Browse files
feat: add solutions to lc problem: No.0419 (doocs#627)
No.0419.Battleships in a Board
1 parent 0ef4530 commit 0faeb29

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

‎solution/0400-0499/0419.Battleships in a Board/README.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,48 @@ XXXX
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def countBattleships(self, board: List[List[str]]) -> int:
55+
m, n = len(board), len(board[0])
56+
res = 0
57+
for i in range(m):
58+
for j in range(n):
59+
if board[i][j] == '.':
60+
continue
61+
if i > 0 and board[i - 1][j] == 'X':
62+
continue
63+
if j > 0 and board[i][j - 1] == 'X':
64+
continue
65+
res += 1
66+
return res
5467
```
5568

5669
### **Java**
5770

5871
<!-- 这里可写当前语言的特殊实现逻辑 -->
5972

6073
```java
61-
74+
class Solution {
75+
public int countBattleships(char[][] board) {
76+
int m = board.length, n = board[0].length;
77+
int res = 0;
78+
for (int i = 0; i < m; i++) {
79+
for (int j = 0; j < n; j++) {
80+
if (board[i][j] == '.') {
81+
continue;
82+
}
83+
if (i > 0 && board[i - 1][j] == 'X') {
84+
continue;
85+
}
86+
if (j > 0 && board[i][j - 1] == 'X') {
87+
continue;
88+
}
89+
res++;
90+
}
91+
}
92+
return res;
93+
}
94+
}
6295
```
6396

6497
### **...**

‎solution/0400-0499/0419.Battleships in a Board/README_EN.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,46 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def countBattleships(self, board: List[List[str]]) -> int:
49+
m, n = len(board), len(board[0])
50+
res = 0
51+
for i in range(m):
52+
for j in range(n):
53+
if board[i][j] == '.':
54+
continue
55+
if i > 0 and board[i - 1][j] == 'X':
56+
continue
57+
if j > 0 and board[i][j - 1] == 'X':
58+
continue
59+
res += 1
60+
return res
4861
```
4962

5063
### **Java**
5164

5265
```java
53-
66+
class Solution {
67+
public int countBattleships(char[][] board) {
68+
int m = board.length, n = board[0].length;
69+
int res = 0;
70+
for (int i = 0; i < m; i++) {
71+
for (int j = 0; j < n; j++) {
72+
if (board[i][j] == '.') {
73+
continue;
74+
}
75+
if (i > 0 && board[i - 1][j] == 'X') {
76+
continue;
77+
}
78+
if (j > 0 && board[i][j - 1] == 'X') {
79+
continue;
80+
}
81+
res++;
82+
}
83+
}
84+
return res;
85+
}
86+
}
5487
```
5588

5689
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int countBattleships(char[][] board) {
3+
int m = board.length, n = board[0].length;
4+
int res = 0;
5+
for (int i = 0; i < m; i++) {
6+
for (int j = 0; j < n; j++) {
7+
if (board[i][j] == '.') {
8+
continue;
9+
}
10+
if (i > 0 && board[i - 1][j] == 'X') {
11+
continue;
12+
}
13+
if (j > 0 && board[i][j - 1] == 'X') {
14+
continue;
15+
}
16+
res++;
17+
}
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countBattleships(self, board: List[List[str]]) -> int:
3+
m, n = len(board), len(board[0])
4+
res = 0
5+
for i in range(m):
6+
for j in range(n):
7+
if board[i][j] == '.':
8+
continue
9+
if i > 0 and board[i - 1][j] == 'X':
10+
continue
11+
if j > 0 and board[i][j - 1] == 'X':
12+
continue
13+
res += 1
14+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /