Skip to content

Commit b92a118

Browse files
committedMar 26, 2020
feat: add python and java solutions to leetcode question
添加LeetCode题解:999. 车的可用捕获量
1 parent c6c14e9 commit b92a118

File tree

8 files changed

+324
-79
lines changed

8 files changed

+324
-79
lines changed
 

‎lcof/面试题33. 二叉搜索树的后序遍历序列/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<!-- 这里写题目描述 -->
55
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 `true`,否则返回 `false`。假设输入的数组的任意两个数字都互不相同。
66

7-
参考以下这颗二叉搜索树
7+
参考以下这棵二叉搜索树
88

99
```
1010
5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [面试题35. 复杂链表的复制](https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
请实现 `copyRandomList` 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 `next` 指针指向下一个节点,还有一个 `random` 指针指向链表中的任意节点或者 `null`
6+
7+
**示例 1:**
8+
9+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e1.png)
10+
11+
```
12+
输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
13+
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
14+
```
15+
16+
**示例 2:**
17+
18+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e2.png)
19+
20+
```
21+
输入:head = [[1,1],[2,1]]
22+
输出:[[1,1],[2,1]]
23+
```
24+
25+
26+
**示例 3:**
27+
28+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e3.png)
29+
30+
```
31+
输入:head = [[3,null],[3,0],[3,null]]
32+
输出:[[3,null],[3,0],[3,null]]
33+
```
34+
35+
**示例 4:**
36+
37+
```
38+
输入:head = []
39+
输出:[]
40+
解释:给定的链表为空(空指针),因此返回 null。
41+
```
42+
43+
**提示:**
44+
45+
- `-10000 <= Node.val <= 10000`
46+
- `Node.random` 为空(null)或指向链表中的节点。
47+
- 节点数目不超过 1000 。
48+
49+
## 解法
50+
<!-- 这里可写通用的实现逻辑 -->
51+
52+
53+
### Python3
54+
<!-- 这里可写当前语言的特殊实现逻辑 -->
55+
56+
```python
57+
58+
```
59+
60+
### Java
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```java
64+
65+
```
66+
67+
### ...
68+
```
69+
70+
```

‎lcof/面试题35. 复杂链表的复制/Solution.java

Whitespace-only changes.

‎lcof/面试题35. 复杂链表的复制/Solution.py

Whitespace-only changes.

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

+46-3
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,63 @@
5353

5454
## 解法
5555
<!-- 这里可写通用的实现逻辑 -->
56-
56+
先找到 R 的位置,之后向“上、下、左、右”四个方向查找,累加结果。
5757

5858
### Python3
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
62+
class Solution:
63+
def numRookCaptures(self, board: List[List[str]]) -> int:
64+
65+
def search(board, i, j, direction):
66+
while i >= 0 and i < 8 and j >= 0 and j < 8:
67+
if board[i][j] == 'B': return 0
68+
if board[i][j] == 'p': return 1
69+
i += direction[0]
70+
j += direction[1]
71+
return 0
72+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
73+
res = 0;
74+
for i in range(8):
75+
for j in range(8):
76+
if board[i][j] == 'R':
77+
for direction in directions:
78+
res += search(board, i, j, direction)
79+
return res
6380
```
6481

6582
### Java
6683
<!-- 这里可写当前语言的特殊实现逻辑 -->
6784

6885
```java
69-
86+
class Solution {
87+
public int numRookCaptures(char[][] board) {
88+
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
89+
int res = 0;
90+
for (int i = 0; i < 8; ++i) {
91+
for (int j = 0; j < 8; ++j) {
92+
if (board[i][j] == 'R') {
93+
for (int[] direction : directions) {
94+
res += search(board, i, j, direction);
95+
}
96+
return res;
97+
}
98+
}
99+
}
100+
return res;
101+
}
102+
103+
private int search(char[][] board, int i, int j, int[] direction) {
104+
while (i >= 0 && i < 8 && j >= 0 && j < 8) {
105+
if (board[i][j] == 'B') return 0;
106+
if (board[i][j] == 'p') return 1;
107+
i += direction[0];
108+
j += direction[1];
109+
}
110+
return 0;
111+
}
112+
}
70113
```
71114

72115
### ...
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,160 @@
1-
# [999. Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook)
2-
3-
## Description
4-
<p>On an 8 x 8 chessboard, there is one white rook.&nbsp; There also may be empty squares, white bishops, and black pawns.&nbsp; These are given as characters &#39;R&#39;, &#39;.&#39;, &#39;B&#39;, and &#39;p&#39; respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.</p>
5-
6-
<p>The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.&nbsp; Also, rooks cannot move into the same square as other friendly bishops.</p>
7-
8-
<p>Return the number of pawns the rook can capture in one move.</p>
9-
10-
<p>&nbsp;</p>
11-
12-
<p><strong>Example 1:</strong></p>
13-
14-
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG" style="width: 300px; height: 305px;" /></p>
15-
16-
<pre>
17-
<strong>Input: </strong><span id="example-input-1-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
18-
<strong>Output: </strong><span id="example-output-1">3</span>
19-
<strong>Explanation: </strong>
20-
In this example the rook is able to capture all the pawns.
21-
</pre>
22-
23-
<p><strong>Example 2:</strong></p>
24-
25-
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG" style="width: 300px; height: 306px;" /></p>
26-
27-
<pre>
28-
<strong>Input: </strong><span id="example-input-2-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;R&quot;,&quot;B&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
29-
<strong>Output: </strong><span id="example-output-2">0</span>
30-
<strong>Explanation: </strong>
31-
Bishops are blocking the rook to capture any pawn.
32-
</pre>
33-
34-
<p><strong>Example 3:</strong></p>
35-
36-
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG" style="width: 300px; height: 305px;" /></p>
37-
38-
<pre>
39-
<strong>Input: </strong><span id="example-input-3-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;B&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
40-
<strong>Output: </strong><span id="example-output-3">3</span>
41-
<strong>Explanation: </strong>
42-
The rook can capture the pawns at positions b5, d6 and f5.
43-
</pre>
44-
45-
<p>&nbsp;</p>
46-
47-
<p><strong>Note:</strong></p>
48-
49-
<ol>
50-
<li><code>board.length == board[i].length == 8</code></li>
51-
<li><code>board[i][j]</code> is either <code>&#39;R&#39;</code>, <code>&#39;.&#39;</code>, <code>&#39;B&#39;</code>, or&nbsp;<code>&#39;p&#39;</code></li>
52-
<li>There is exactly one cell with <code>board[i][j] == &#39;R&#39;</code></li>
53-
</ol>
54-
55-
56-
57-
## Solutions
58-
59-
60-
### Python3
61-
62-
```python
63-
64-
```
65-
66-
### Java
67-
68-
```java
69-
70-
```
71-
72-
### ...
73-
```
74-
75-
```
1+
# [999. Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook)
2+
3+
## Description
4+
<p>On an 8 x 8 chessboard, there is one white rook.&nbsp; There also may be empty squares, white bishops, and black pawns.&nbsp; These are given as characters &#39;R&#39;, &#39;.&#39;, &#39;B&#39;, and &#39;p&#39; respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.</p>
5+
6+
7+
8+
<p>The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.&nbsp; Also, rooks cannot move into the same square as other friendly bishops.</p>
9+
10+
11+
12+
<p>Return the number of pawns the rook can capture in one move.</p>
13+
14+
15+
16+
<p>&nbsp;</p>
17+
18+
19+
20+
<p><strong>Example 1:</strong></p>
21+
22+
23+
24+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG" style="width: 300px; height: 305px;" /></p>
25+
26+
27+
28+
<pre>
29+
30+
<strong>Input: </strong><span id="example-input-1-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
31+
32+
<strong>Output: </strong><span id="example-output-1">3</span>
33+
34+
<strong>Explanation: </strong>
35+
36+
In this example the rook is able to capture all the pawns.
37+
38+
</pre>
39+
40+
41+
42+
<p><strong>Example 2:</strong></p>
43+
44+
45+
46+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG" style="width: 300px; height: 306px;" /></p>
47+
48+
49+
50+
<pre>
51+
52+
<strong>Input: </strong><span id="example-input-2-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;R&quot;,&quot;B&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
53+
54+
<strong>Output: </strong><span id="example-output-2">0</span>
55+
56+
<strong>Explanation: </strong>
57+
58+
Bishops are blocking the rook to capture any pawn.
59+
60+
</pre>
61+
62+
63+
64+
<p><strong>Example 3:</strong></p>
65+
66+
67+
68+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG" style="width: 300px; height: 305px;" /></p>
69+
70+
71+
72+
<pre>
73+
74+
<strong>Input: </strong><span id="example-input-3-1">[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;B&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]</span>
75+
76+
<strong>Output: </strong><span id="example-output-3">3</span>
77+
78+
<strong>Explanation: </strong>
79+
80+
The rook can capture the pawns at positions b5, d6 and f5.
81+
82+
</pre>
83+
84+
85+
86+
<p>&nbsp;</p>
87+
88+
89+
90+
<p><strong>Note:</strong></p>
91+
92+
1. `board.length == board[i].length == 8`
93+
2. `board[i][j]` is either `'R'`, `'.'`, `'B'` or `'p'`
94+
3. There is exactly one cell with `board[i][j] == 'R'`
95+
96+
97+
98+
99+
## Solutions
100+
101+
102+
### Python3
103+
104+
```python
105+
class Solution:
106+
def numRookCaptures(self, board: List[List[str]]) -> int:
107+
108+
def search(board, i, j, direction):
109+
while i >= 0 and i < 8 and j >= 0 and j < 8:
110+
if board[i][j] == 'B': return 0
111+
if board[i][j] == 'p': return 1
112+
i += direction[0]
113+
j += direction[1]
114+
return 0
115+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
116+
res = 0;
117+
for i in range(8):
118+
for j in range(8):
119+
if board[i][j] == 'R':
120+
for direction in directions:
121+
res += search(board, i, j, direction)
122+
return res
123+
```
124+
125+
### Java
126+
127+
```java
128+
class Solution {
129+
public int numRookCaptures(char[][] board) {
130+
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
131+
int res = 0;
132+
for (int i = 0; i < 8; ++i) {
133+
for (int j = 0; j < 8; ++j) {
134+
if (board[i][j] == 'R') {
135+
for (int[] direction : directions) {
136+
res += search(board, i, j, direction);
137+
}
138+
return res;
139+
}
140+
}
141+
}
142+
return res;
143+
}
144+
145+
private int search(char[][] board, int i, int j, int[] direction) {
146+
while (i >= 0 && i < 8 && j >= 0 && j < 8) {
147+
if (board[i][j] == 'B') return 0;
148+
if (board[i][j] == 'p') return 1;
149+
i += direction[0];
150+
j += direction[1];
151+
}
152+
return 0;
153+
}
154+
}
155+
```
156+
157+
### ...
158+
```
159+
160+
```

0 commit comments

Comments
 (0)