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 c3d1e71

Browse files
committed
feat: add solutions to lc problem: No.0999
No.0999.Available Captures for Rook
1 parent 68c0018 commit c3d1e71

File tree

18 files changed

+556
-113
lines changed

18 files changed

+556
-113
lines changed

‎README.md‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@
212212
- [子集](./solution/0000-0099/0078.Subsets/README.md)
213213
- [子集 II](./solution/0000-0099/0090.Subsets%20II/README.md)
214214

215+
### 并查集
216+
217+
- [被围绕的区域](./solution/0100-0199/0130.Surrounded%20Regions/README.md)
218+
- [省份数量](./solution/0500-0599/0547.Number%20of%20Provinces/README.md)
219+
- [冗余连接](./solution/0600-0699/0684.Redundant%20Connection/README.md)
220+
215221
### 设计
216222

217223
- [LRU 缓存机制](./solution/0100-0199/0146.Lru%20Cache/README.md)
@@ -222,13 +228,6 @@
222228
- [设计哈希集合](./solution/0700-0799/0705.Design%20HashSet/README.md)
223229
- [设计哈希映射](./solution/0700-0799/0706.Design%20HashMap/README.md)
224230

225-
### 数据库
226-
227-
- [组合两个表](./solution/0100-0199/0175.Combine%20Two%20Tables/README.md)
228-
- [第二高的薪水](./solution/0100-0199/0176.Second%20Highest%20Salary/README.md)
229-
- [第 N 高的薪水](./solution/0100-0199/0177.Nth%20Highest%20Salary/README.md)
230-
- [分数排名](./solution/0100-0199/0178.Rank%20Scores/README.md)
231-
232231
## 维护者
233232

234233
- [Yang Libin](https://github.com/yanglbme)

‎README_EN.md‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
204204
- [Subsets](./solution/0000-0099/0078.Subsets/README_EN.md)
205205
- [Subsets II](./solution/0000-0099/0090.Subsets%20II/README_EN.md)
206206

207+
### Union Find
208+
209+
- [Surrounded Regions](./solution/0100-0199/0130.Surrounded%20Regions/README_EN.md)
210+
- [Number of Provinces](./solution/0500-0599/0547.Number%20of%20Provinces/README_EN.md)
211+
- [Redundant Connection](./solution/0600-0699/0684.Redundant%20Connection/README_EN.md)
212+
207213
### Design
208214

209215
- [LRU Cache](./solution/0100-0199/0146.Lru%20Cache/README_EN.md)
@@ -214,13 +220,6 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
214220
- [Design HashSet](./solution/0700-0799/0705.Design%20HashSet/README_EN.md)
215221
- [Design HashMap](./solution/0700-0799/0706.Design%20HashMap/README_EN.md)
216222

217-
### Database
218-
219-
- [Combine Two Tables](./solution/0100-0199/0175.Combine%20Two%20Tables/README_EN.md)
220-
- [Second Highest Salary](./solution/0100-0199/0176.Second%20Highest%20Salary/README_EN.md)
221-
- [Nth Highest Salary](./solution/0100-0199/0177.Nth%20Highest%20Salary/README_EN.md)
222-
- [Rank Scores](./solution/0100-0199/0178.Rank%20Scores/README_EN.md)
223-
224223
## Maintainer
225224

226225
- [Yang Libin](https://github.com/yanglbme)

‎solution/0600-0699/0684.Redundant Connection/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Solution:
9393
if p[x] != x:
9494
p[x] = find(p[x])
9595
return p[x]
96-
96+
9797
for a, b in edges:
9898
if find(a) == find(b):
9999
return [a, b]

‎solution/0900-0999/0999.Available Captures for Rook/README.md‎

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
<li>只有一个格子上存在&nbsp;<code>board[i][j] == &#39;R&#39;</code></li>
6262
</ol>
6363

64-
6564
## 解法
6665

6766
<!-- 这里可写通用的实现逻辑 -->
@@ -77,22 +76,21 @@
7776
```python
7877
class Solution:
7978
def numRookCaptures(self, board: List[List[str]]) -> int:
80-
81-
def search(board, i, j, direction):
82-
while i >= 0 and i < 8 and j >= 0 and j < 8:
83-
if board[i][j] == 'B': return 0
84-
if board[i][j] == 'p': return 1
85-
i += direction[0]
86-
j += direction[1]
87-
return 0
88-
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
89-
res = 0;
90-
for i in range(8):
91-
for j in range(8):
79+
x, y, n = 0, 0, 8
80+
for i in range(n):
81+
for j in range(n):
9282
if board[i][j] == 'R':
93-
for direction in directions:
94-
res += search(board, i, j, direction)
95-
return res
83+
x, y = i, j
84+
break
85+
ans = 0
86+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
87+
i, j = x, y
88+
while 0 <= i + a < n and 0 <= j + b < n and board[i + a][j + b] != 'B':
89+
i, j = i + a, j + b
90+
if board[i][j] == 'p':
91+
ans += 1
92+
break
93+
return ans
9694
```
9795

9896
### **Java**
@@ -102,30 +100,112 @@ class Solution:
102100
```java
103101
class Solution {
104102
public int numRookCaptures(char[][] board) {
105-
int[][] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
106-
int res = 0;
107-
for (int i = 0; i < 8; ++i) {
108-
for (int j = 0; j < 8; ++j) {
103+
int[] pos = find(board);
104+
int ans = 0, n = 8;
105+
int[][] dirs = new int[][]{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
106+
for (int[] dir : dirs) {
107+
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
108+
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
109+
x += a;
110+
y += b;
111+
if (board[x][y] == 'p') {
112+
++ans;
113+
break;
114+
}
115+
}
116+
}
117+
return ans;
118+
}
119+
120+
private int[] find(char[][] board) {
121+
int n = 8;
122+
for (int i = 0; i < n; ++i) {
123+
for (int j = 0; j < n; ++j) {
109124
if (board[i][j] == 'R') {
110-
for (int[] direction : directions) {
111-
res += search(board, i, j, direction);
112-
}
113-
return res;
125+
return new int[]{i, j};
126+
}
127+
}
128+
}
129+
return null;
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
int numRookCaptures(vector<vector<char>>& board) {
140+
vector<int> pos = find(board);
141+
int ans = 0, n = 8;
142+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
143+
for (auto& dir : dirs)
144+
{
145+
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
146+
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B')
147+
{
148+
x += a;
149+
y += b;
150+
if (board[x][y] == 'p')
151+
{
152+
++ans;
153+
break;
114154
}
115155
}
116156
}
117-
return res;
157+
return ans;
118158
}
119159

120-
private int search(char[][] board, int i, int j, int[] direction) {
121-
while (i >= 0 && i < 8 && j >= 0 && j < 8) {
122-
if (board[i][j] == 'B') return 0;
123-
if (board[i][j] == 'p') return 1;
124-
i += direction[0];
125-
j += direction[1];
160+
vector<int> find(vector<vector<char>>& board) {
161+
int n = 8;
162+
for (int i = 0; i < n; ++i)
163+
{
164+
for (int j = 0; j < n; ++j)
165+
{
166+
if (board[i][j] == 'R')
167+
{
168+
return {i, j};
169+
}
170+
}
126171
}
127-
return 0;
172+
return {};
128173
}
174+
};
175+
```
176+
177+
### **Go**
178+
179+
```go
180+
func numRookCaptures(board [][]byte) int {
181+
n := 8
182+
183+
find := func() []int {
184+
for i := 0; i < n; i++ {
185+
for j := 0; j < n; j++ {
186+
if board[i][j] == 'R' {
187+
return []int{i, j}
188+
}
189+
}
190+
}
191+
return []int{}
192+
}
193+
194+
pos := find()
195+
ans := 0
196+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
197+
for _, dir := range dirs {
198+
x, y, a, b := pos[0], pos[1], dir[0], dir[1]
199+
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != 'B' {
200+
x += a
201+
y += b
202+
if board[x][y] == 'p' {
203+
ans++
204+
break
205+
}
206+
}
207+
}
208+
return ans
129209
}
130210
```
131211

0 commit comments

Comments
(0)

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