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 fd65b5e

Browse files
committed
feat: add solutions to lc problems: No.0419,0526,1041
* No.0419.Battleships in a Board * No.0526.Beautiful Arrangement * No.1041.Robot Bounded In Circle
1 parent a167b03 commit fd65b5e

File tree

17 files changed

+709
-69
lines changed

17 files changed

+709
-69
lines changed

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

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ XXXX
3838

3939
<p>你可以用<strong>一次扫描算法</strong>,只使用<strong>O(1)额外空间,</strong>并且<strong>不修改</strong>甲板的值来解决这个问题吗?</p>
4040

41-
4241
## 解法
4342

4443
<!-- 这里可写通用的实现逻辑 -->
4544

45+
初始化结果数 ans = 0。
46+
47+
遍历二维甲板,若 X 的左方、上方不为 X,则结果 ans 加 1。
48+
4649
<!-- tabs:start -->
4750

4851
### **Python3**
@@ -53,7 +56,7 @@ XXXX
5356
class Solution:
5457
def countBattleships(self, board: List[List[str]]) -> int:
5558
m, n = len(board), len(board[0])
56-
res = 0
59+
ans = 0
5760
for i in range(m):
5861
for j in range(n):
5962
if board[i][j] == '.':
@@ -62,8 +65,8 @@ class Solution:
6265
continue
6366
if j > 0 and board[i][j - 1] == 'X':
6467
continue
65-
res += 1
66-
return res
68+
ans += 1
69+
return ans
6770
```
6871

6972
### **Java**
@@ -74,9 +77,9 @@ class Solution:
7477
class Solution {
7578
public int countBattleships(char[][] board) {
7679
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+
int ans = 0;
81+
for (int i = 0; i < m; ++i) {
82+
for (int j = 0; j < n; ++j) {
8083
if (board[i][j] == '.') {
8184
continue;
8285
}
@@ -86,14 +89,61 @@ class Solution {
8689
if (j > 0 && board[i][j - 1] == 'X') {
8790
continue;
8891
}
89-
res++;
92+
++ans;
9093
}
9194
}
92-
return res;
95+
return ans;
9396
}
9497
}
9598
```
9699

100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int countBattleships(vector<vector<char>>& board) {
106+
int m = board.size(), n = board[0].size();
107+
int ans = 0;
108+
for (int i = 0; i < m; ++i)
109+
{
110+
for (int j = 0; j < n; ++j)
111+
{
112+
if (board[i][j] == '.') continue;
113+
if (i > 0 && board[i - 1][j] == 'X') continue;
114+
if (j > 0 && board[i][j - 1] == 'X') continue;
115+
++ans;
116+
}
117+
}
118+
return ans;
119+
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func countBattleships(board [][]byte) int {
127+
m, n := len(board), len(board[0])
128+
ans := 0
129+
for i := 0; i < m; i++ {
130+
for j := 0; j < n; j++ {
131+
if board[i][j] == '.' {
132+
continue
133+
}
134+
if i > 0 && board[i-1][j] == 'X' {
135+
continue
136+
}
137+
if j > 0 && board[i][j-1] == 'X' {
138+
continue
139+
}
140+
ans++
141+
}
142+
}
143+
return ans
144+
}
145+
```
146+
97147
### **...**
98148

99149
```

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

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<p>&nbsp;</p>
3737
<p><strong>Follow up:</strong> Could you do it in one-pass, using only <code>O(1)</code> extra memory and without modifying the values <code>board</code>?</p>
3838

39-
4039
## Solutions
4140

4241
<!-- tabs:start -->
@@ -47,7 +46,7 @@
4746
class Solution:
4847
def countBattleships(self, board: List[List[str]]) -> int:
4948
m, n = len(board), len(board[0])
50-
res = 0
49+
ans = 0
5150
for i in range(m):
5251
for j in range(n):
5352
if board[i][j] == '.':
@@ -56,8 +55,8 @@ class Solution:
5655
continue
5756
if j > 0 and board[i][j - 1] == 'X':
5857
continue
59-
res += 1
60-
return res
58+
ans += 1
59+
return ans
6160
```
6261

6362
### **Java**
@@ -66,9 +65,9 @@ class Solution:
6665
class Solution {
6766
public int countBattleships(char[][] board) {
6867
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++) {
68+
int ans = 0;
69+
for (int i = 0; i < m; ++i) {
70+
for (int j = 0; j < n; ++j) {
7271
if (board[i][j] == '.') {
7372
continue;
7473
}
@@ -78,14 +77,61 @@ class Solution {
7877
if (j > 0 && board[i][j - 1] == 'X') {
7978
continue;
8079
}
81-
res++;
80+
++ans;
8281
}
8382
}
84-
return res;
83+
return ans;
8584
}
8685
}
8786
```
8887

88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int countBattleships(vector<vector<char>>& board) {
94+
int m = board.size(), n = board[0].size();
95+
int ans = 0;
96+
for (int i = 0; i < m; ++i)
97+
{
98+
for (int j = 0; j < n; ++j)
99+
{
100+
if (board[i][j] == '.') continue;
101+
if (i > 0 && board[i - 1][j] == 'X') continue;
102+
if (j > 0 && board[i][j - 1] == 'X') continue;
103+
++ans;
104+
}
105+
}
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func countBattleships(board [][]byte) int {
115+
m, n := len(board), len(board[0])
116+
ans := 0
117+
for i := 0; i < m; i++ {
118+
for j := 0; j < n; j++ {
119+
if board[i][j] == '.' {
120+
continue
121+
}
122+
if i > 0 && board[i-1][j] == 'X' {
123+
continue
124+
}
125+
if j > 0 && board[i][j-1] == 'X' {
126+
continue
127+
}
128+
ans++
129+
}
130+
}
131+
return ans
132+
}
133+
```
134+
89135
### **...**
90136

91137
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int countBattleships(vector<vector<char>>& board) {
4+
int m = board.size(), n = board[0].size();
5+
int ans = 0;
6+
for (int i = 0; i < m; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
if (board[i][j] == '.') continue;
11+
if (i > 0 && board[i - 1][j] == 'X') continue;
12+
if (j > 0 && board[i][j - 1] == 'X') continue;
13+
++ans;
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func countBattleships(board [][]byte) int {
2+
m, n := len(board), len(board[0])
3+
ans := 0
4+
for i := 0; i < m; i++ {
5+
for j := 0; j < n; j++ {
6+
if board[i][j] == '.' {
7+
continue
8+
}
9+
if i > 0 && board[i-1][j] == 'X' {
10+
continue
11+
}
12+
if j > 0 && board[i][j-1] == 'X' {
13+
continue
14+
}
15+
ans++
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public int countBattleships(char[][] board) {
33
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++) {
4+
int ans = 0;
5+
for (int i = 0; i < m; ++i) {
6+
for (int j = 0; j < n; ++j) {
77
if (board[i][j] == '.') {
88
continue;
99
}
@@ -13,9 +13,9 @@ public int countBattleships(char[][] board) {
1313
if (j > 0 && board[i][j - 1] == 'X') {
1414
continue;
1515
}
16-
res++;
16+
++ans;
1717
}
1818
}
19-
return res;
19+
return ans;
2020
}
2121
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def countBattleships(self, board: List[List[str]]) -> int:
33
m, n = len(board), len(board[0])
4-
res = 0
4+
ans = 0
55
for i in range(m):
66
for j in range(n):
77
if board[i][j] == '.':
@@ -10,5 +10,5 @@ def countBattleships(self, board: List[List[str]]) -> int:
1010
continue
1111
if j > 0 and board[i][j - 1] == 'X':
1212
continue
13-
res += 1
14-
return res
13+
ans += 1
14+
return ans

0 commit comments

Comments
(0)

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