Skip to content

Commit 8e2a2e6

Browse files
authored
feat: update solutions to lcci problems: No.16.01,16.25 (doocs#2708)
1 parent 659f685 commit 8e2a2e6

File tree

20 files changed

+1365
-153
lines changed

20 files changed

+1365
-153
lines changed

Diff for: lcci/16.01.Swap Numbers/README.md

-13
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,4 @@ function swapNumbers(numbers: number[]): number[] {
9191

9292
<!-- tabs:end -->
9393

94-
### 方法二
95-
96-
<!-- tabs:start -->
97-
98-
```ts
99-
function swapNumbers(numbers: number[]): number[] {
100-
[numbers[0], numbers[1]] = [numbers[1], numbers[0]];
101-
return numbers;
102-
}
103-
```
104-
105-
<!-- tabs:end -->
106-
10794
<!-- end -->

Diff for: lcci/16.01.Swap Numbers/README_EN.md

-13
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,4 @@ function swapNumbers(numbers: number[]): number[] {
9898

9999
<!-- tabs:end -->
100100

101-
### Solution 2
102-
103-
<!-- tabs:start -->
104-
105-
```ts
106-
function swapNumbers(numbers: number[]): number[] {
107-
[numbers[0], numbers[1]] = [numbers[1], numbers[0]];
108-
return numbers;
109-
}
110-
```
111-
112-
<!-- tabs:end -->
113-
114101
<!-- end -->

Diff for: lcci/16.01.Swap Numbers/Solution2.ts

-4
This file was deleted.

Diff for: lcci/16.15.Master Mind/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
### 方法一:哈希表
2525

26-
同时遍历两个字符串,算出对应位置字符相同的个数,累加到 $x$ 中,然后将两个字符串出现的字符以及出现的次数分别记录在哈希表 $cnt1$ 和 $cnt2$ 中。
26+
我们同时遍历两个字符串,算出对应位置字符相同的个数,累加到 $x$ 中,然后将两个字符串出现的字符以及出现的次数分别记录在哈希表 $cnt1$ 和 $cnt2$ 中。
2727

2828
接着遍历两个哈希表,算出有多少共同出现的字符,累加到 $y$ 中。那么答案就是 $[x, y - x]$。
2929

Diff for: lcci/16.20.T9/README.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class Solution:
4545
return [w for w in words if check(w)]
4646
```
4747

48+
```python
49+
class Solution:
50+
def getValidT9Words(self, num: str, words: List[str]) -> List[str]:
51+
trans = str.maketrans(ascii_lowercase, "22233344455566677778889999")
52+
return [w for w in words if w.translate(trans) == num]
53+
```
54+
4855
```java
4956
class Solution {
5057
public List<String> getValidT9Words(String num, String[] words) {
@@ -149,17 +156,4 @@ function getValidT9Words(num: string, words: string[]): string[] {
149156

150157
<!-- tabs:end -->
151158

152-
### 方法二
153-
154-
<!-- tabs:start -->
155-
156-
```python
157-
class Solution:
158-
def getValidT9Words(self, num: str, words: List[str]) -> List[str]:
159-
trans = str.maketrans(ascii_lowercase, "22233344455566677778889999")
160-
return [w for w in words if w.translate(trans) == num]
161-
```
162-
163-
<!-- tabs:end -->
164-
165159
<!-- end -->

Diff for: lcci/16.20.T9/README_EN.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class Solution:
5151
return [w for w in words if check(w)]
5252
```
5353

54+
```python
55+
class Solution:
56+
def getValidT9Words(self, num: str, words: List[str]) -> List[str]:
57+
trans = str.maketrans(ascii_lowercase, "22233344455566677778889999")
58+
return [w for w in words if w.translate(trans) == num]
59+
```
60+
5461
```java
5562
class Solution {
5663
public List<String> getValidT9Words(String num, String[] words) {
@@ -155,17 +162,4 @@ function getValidT9Words(num: string, words: string[]): string[] {
155162

156163
<!-- tabs:end -->
157164

158-
### Solution 2
159-
160-
<!-- tabs:start -->
161-
162-
```python
163-
class Solution:
164-
def getValidT9Words(self, num: str, words: List[str]) -> List[str]:
165-
trans = str.maketrans(ascii_lowercase, "22233344455566677778889999")
166-
return [w for w in words if w.translate(trans) == num]
167-
```
168-
169-
<!-- tabs:end -->
170-
171165
<!-- end -->

Diff for: lcci/16.22.Langtons Ant/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141

4242
### 方法一:哈希表 + 模拟
4343

44-
我们使用哈希表 $black$ 来记录所有黑色方格的位置,哈希表 $dirs$ 来记录蚂蚁的四个方向。我们使用变量 $x, y$ 来记录蚂蚁的位置,使用变量 $p$ 来记录蚂蚁的方向。我们使用变量 $x1, y1, x2, y2$ 来记录所有黑色方格的最小横坐标、最小纵坐标、最大横坐标、最大纵坐标。
44+
我们使用哈希表 $\textit{black}$ 来记录所有黑色方格的位置,哈希表 $\textit{dirs}$ 来记录蚂蚁的四个方向。我们使用变量 $x$, $y$ 来记录蚂蚁的位置,使用变量 $p$ 来记录蚂蚁的方向。我们使用变量 $x_1$, $y_1$, $x_2$, $y_2$ 来记录所有黑色方格的最小横坐标、最小纵坐标、最大横坐标、最大纵坐标。
4545

46-
我们模拟蚂蚁的行走过程。如果蚂蚁所在的方格是白色的,那么蚂蚁向右转 $90$ 度,将方格涂黑,向前移动一个单位。如果蚂蚁所在的方格是黑色的,那么蚂蚁向左转 $90$ 度,将方格涂白,向前移动一个单位。在模拟的过程中,我们不断更新 $x1, y1, x2, y2$ 的值,使得它们能够包含蚂蚁走过的所有方格。
46+
我们模拟蚂蚁的行走过程。如果蚂蚁所在的方格是白色的,那么蚂蚁向右转 $90$ 度,将方格涂黑,向前移动一个单位。如果蚂蚁所在的方格是黑色的,那么蚂蚁向左转 $90$ 度,将方格涂白,向前移动一个单位。在模拟的过程中,我们不断更新 $x_1$, $y_1$, $x_2$, $y_2$ 的值,使得它们能够包含蚂蚁走过的所有方格。
4747

48-
模拟结束后,我们根据 $x1, y1, x2, y2$ 的值,构造出答案矩阵 $g$。然后,我们将蚂蚁所在的位置涂上蚂蚁的方向,同时将所有黑色方格涂上 $X$,最后返回答案矩阵。
48+
模拟结束后,我们根据 $x_1$, $y_1$, $x_2$, $y_2$ 的值,构造出答案矩阵 $g$。然后,我们将蚂蚁所在的位置涂上蚂蚁的方向,同时将所有黑色方格涂上 $X$,最后返回答案矩阵。
4949

5050
时间复杂度 $O(K)$,空间复杂度 $O(K)$。其中 $K$ 是蚂蚁行走的步数。
5151

Diff for: lcci/16.22.Langtons Ant/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@
6161

6262
### Solution 1: Hash Table + Simulation
6363

64-
We use a hash table $black$ to record the positions of all black squares, and a hash table $dirs$ to record the four directions of the ant. We use variables $x, y$ to record the position of the ant, and variable $p$ to record the direction of the ant. We use variables $x1, y1, x2, y2$ to record the minimum horizontal coordinate, minimum vertical coordinate, maximum horizontal coordinate, and maximum vertical coordinate of all black squares.
64+
We use a hash table `black` to record the positions of all black squares, and a hash table `dirs` to record the four directions of the ant. We use variables $x$, $y$ to record the position of the ant, and variable $p$ to record the direction of the ant. We use variables $x_1$, $y_1$, $x_2$, $y_2$ to record the minimum x-coordinate, minimum y-coordinate, maximum x-coordinate, and maximum y-coordinate of all black squares, respectively.
6565

66-
We simulate the ant's walking process. If the square where the ant is located is white, the ant turns right by $90$ degrees, paints the square black, and moves forward one unit. If the square where the ant is located is black, the ant turns left by $90$ degrees, paints the square white, and moves forward one unit. During the simulation, we continuously update the values of $x1, y1, x2, y2$ so that they can contain all the squares the ant has walked through.
66+
We simulate the walking process of the ant. If the square where the ant is located is white, then the ant turns right by $90$ degrees, paints the square black, and moves forward one unit. If the square where the ant is located is black, then the ant turns left by $90$ degrees, paints the square white, and moves forward one unit. During the simulation process, we continuously update the values of $x_1$, $y_1$, $x_2$, $y_2$ so that they can include all the squares that the ant has walked through.
6767

68-
After the simulation is over, we construct the answer matrix $g$ based on the values of $x1, y1, x2, y2$. Then, we paint the direction of the ant on the square where the ant is located, paint all black squares with $X$, and finally return the answer matrix.
68+
After the simulation, we construct the answer matrix $g$ based on the values of $x_1$, $y_1$, $x_2$, $y_2$. Then, we paint the direction of the ant at the ant's position, and paint all black squares with $X$, and finally return the answer matrix.
6969

70-
The time complexity is $O(K)$, and the space complexity is $O(K)$. Here, $K$ is the number of steps the ant walks.
70+
The time complexity is $O(K)$, and the space complexity is $O(K)$. Where $K$ is the number of steps the ant walks.
7171

7272
<!-- tabs:start -->
7373

0 commit comments

Comments
 (0)