Skip to content

Commit 0e3d915

Browse files
committed
Merge branch 'main' of github.com:doocs/leetcode into feature/lc-1769
2 parents ee246f4 + e27eeca commit 0e3d915

File tree

61 files changed

+26227
-23570
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+26227
-23570
lines changed

images/starcharts.svg

Lines changed: 23590 additions & 23480 deletions
Loading

lcp/LCP 56. 信物传送/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,57 @@ function conveyorBelt(matrix: string[], start: number[], end: number[]): number
271271
}
272272
```
273273

274+
#### Swift
275+
276+
```swift
277+
class Solution {
278+
func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int {
279+
let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)]
280+
let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3]
281+
282+
let rows = matrix.count
283+
let cols = matrix[0].count
284+
285+
var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows)
286+
var deque: [(Int, Int)] = []
287+
288+
dist[start[0]][start[1]] = 0
289+
deque.append((start[0], start[1]))
290+
291+
while !deque.isEmpty {
292+
let (i, j) = deque.removeFirst()
293+
294+
if i == end[0] && j == end[1] {
295+
return dist[i][j]
296+
}
297+
298+
for (k, (di, dj)) in directions.enumerated() {
299+
let ni = i + di
300+
let nj = j + dj
301+
302+
if ni >= 0 && ni < rows && nj >= 0 && nj < cols {
303+
let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)]
304+
let additionalCost = directionMap[currentChar] == k ? 0 : 1
305+
let newDist = dist[i][j] + additionalCost
306+
307+
if newDist < dist[ni][nj] {
308+
dist[ni][nj] = newDist
309+
310+
if additionalCost == 0 {
311+
deque.insert((ni, nj), at: 0)
312+
} else {
313+
deque.append((ni, nj))
314+
}
315+
}
316+
}
317+
}
318+
}
319+
320+
return -1
321+
}
322+
}
323+
```
324+
274325
<!-- tabs:end -->
275326

276327
<!-- solution:end -->
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int {
3+
let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)]
4+
let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3]
5+
6+
let rows = matrix.count
7+
let cols = matrix[0].count
8+
9+
var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows)
10+
var deque: [(Int, Int)] = []
11+
12+
dist[start[0]][start[1]] = 0
13+
deque.append((start[0], start[1]))
14+
15+
while !deque.isEmpty {
16+
let (i, j) = deque.removeFirst()
17+
18+
if i == end[0] && j == end[1] {
19+
return dist[i][j]
20+
}
21+
22+
for (k, (di, dj)) in directions.enumerated() {
23+
let ni = i + di
24+
let nj = j + dj
25+
26+
if ni >= 0 && ni < rows && nj >= 0 && nj < cols {
27+
let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)]
28+
let additionalCost = directionMap[currentChar] == k ? 0 : 1
29+
let newDist = dist[i][j] + additionalCost
30+
31+
if newDist < dist[ni][nj] {
32+
dist[ni][nj] = newDist
33+
34+
if additionalCost == 0 {
35+
deque.insert((ni, nj), at: 0)
36+
} else {
37+
deque.append((ni, nj))
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
return -1
45+
}
46+
}

lcp/LCP 61. 气温变化趋势/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,31 @@ impl Solution {
183183
}
184184
```
185185

186+
#### Swift
187+
188+
```swift
189+
class Solution {
190+
func temperatureTrend(_ temperatureA: [Int], _ temperatureB: [Int]) -> Int {
191+
var maxTrend = 0
192+
var currentTrend = 0
193+
194+
for i in 0..<temperatureA.count - 1 {
195+
let changeA = temperatureA[i + 1] - temperatureA[i]
196+
let changeB = temperatureB[i + 1] - temperatureB[i]
197+
198+
if (changeA == 0 && changeB == 0) || (changeA * changeB > 0) {
199+
currentTrend += 1
200+
maxTrend = max(maxTrend, currentTrend)
201+
} else {
202+
currentTrend = 0
203+
}
204+
}
205+
206+
return maxTrend
207+
}
208+
}
209+
```
210+
186211
<!-- tabs:end -->
187212

188213
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func temperatureTrend(_ temperatureA: [Int], _ temperatureB: [Int]) -> Int {
3+
var maxTrend = 0
4+
var currentTrend = 0
5+
6+
for i in 0..<temperatureA.count - 1 {
7+
let changeA = temperatureA[i + 1] - temperatureA[i]
8+
let changeB = temperatureB[i + 1] - temperatureB[i]
9+
10+
if (changeA == 0 && changeB == 0) || (changeA * changeB > 0) {
11+
currentTrend += 1
12+
maxTrend = max(maxTrend, currentTrend)
13+
} else {
14+
currentTrend = 0
15+
}
16+
}
17+
18+
return maxTrend
19+
}
20+
}

solution/0700-0799/0753.Cracking the Safe/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ func crackSafe(n int, k int) string {
189189
}
190190
```
191191

192+
#### TypeScript
193+
194+
```ts
195+
function crackSafe(n: number, k: number): string {
196+
function dfs(u: number): void {
197+
for (let x = 0; x < k; x++) {
198+
const e = u * 10 + x;
199+
if (!vis.has(e)) {
200+
vis.add(e);
201+
const v = e % mod;
202+
dfs(v);
203+
ans.push(x.toString());
204+
}
205+
}
206+
}
207+
208+
const mod = Math.pow(10, n - 1);
209+
const vis = new Set<number>();
210+
const ans: string[] = [];
211+
212+
dfs(0);
213+
ans.push('0'.repeat(n - 1));
214+
return ans.join('');
215+
}
216+
```
217+
192218
<!-- tabs:end -->
193219

194220
<!-- solution:end -->

solution/0700-0799/0753.Cracking the Safe/README_EN.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ Thus &quot;01100&quot; will unlock the safe. &quot;10011&quot;, and &quot;11001&
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Eulerian Circuit
80+
81+
We can construct a directed graph based on the description in the problem: each point is considered as a length $n-1$ $k$-string, and each edge carries a character from $0$ to $k-1$. If there is a directed edge $e$ from point $u$ to point $v$, and the character carried by $e$ is $c$, then the last $k-1$ characters of $u+c$ form the string $v$. At this point, the edge $u+c$ represents a password of length $n$.
82+
83+
In this directed graph, there are $k^{n-1}$ points, each point has $k$ outgoing edges and $k$ incoming edges. Therefore, this directed graph has an Eulerian circuit, and the path traversed by the Eulerian circuit is the answer to the problem.
84+
85+
The time complexity is $O(k^n)$, and the space complexity is $O(k^n)$.
8086

8187
<!-- tabs:start -->
8288

@@ -181,6 +187,32 @@ func crackSafe(n int, k int) string {
181187
}
182188
```
183189

190+
#### TypeScript
191+
192+
```ts
193+
function crackSafe(n: number, k: number): string {
194+
function dfs(u: number): void {
195+
for (let x = 0; x < k; x++) {
196+
const e = u * 10 + x;
197+
if (!vis.has(e)) {
198+
vis.add(e);
199+
const v = e % mod;
200+
dfs(v);
201+
ans.push(x.toString());
202+
}
203+
}
204+
}
205+
206+
const mod = Math.pow(10, n - 1);
207+
const vis = new Set<number>();
208+
const ans: string[] = [];
209+
210+
dfs(0);
211+
ans.push('0'.repeat(n - 1));
212+
return ans.join('');
213+
}
214+
```
215+
184216
<!-- tabs:end -->
185217

186218
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function crackSafe(n: number, k: number): string {
2+
function dfs(u: number): void {
3+
for (let x = 0; x < k; x++) {
4+
const e = u * 10 + x;
5+
if (!vis.has(e)) {
6+
vis.add(e);
7+
const v = e % mod;
8+
dfs(v);
9+
ans.push(x.toString());
10+
}
11+
}
12+
}
13+
14+
const mod = Math.pow(10, n - 1);
15+
const vis = new Set<number>();
16+
const ans: string[] = [];
17+
18+
dfs(0);
19+
ans.push('0'.repeat(n - 1));
20+
return ans.join('');
21+
}

solution/0700-0799/0754.Reach a Number/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ tags:
6868

6969
### 方法一:数学分析
7070

71-
由于对称性,每次可以选择向左或向右移动,因此,我们可以将 $target$ 统一取绝对值。
71+
由于对称性,每次可以选择向左或向右移动,因此,我们可以将 $\textit{target}$ 统一取绝对值。
7272

7373
定义 $s$ 表示当前所处的位置,用变量 $k$ 记录移动的次数。初始时 $s$ 和 $k$ 均为 $0$。
7474

75-
我们将 $s$ 一直循环累加,直到满足 $s\ge target$ 并且 $(s-target)\mod 2 = 0$,此时的移动次数 $k$ 就是答案,直接返回。
75+
我们将 $s$ 一直循环累加,直到满足 $s\ge \textit{target}$ 并且 $(s-\textit{target}) \bmod 2 = 0$,此时的移动次数 $k$ 就是答案,直接返回。
7676

77-
为什么?因为如果 $s\ge target$ 且 $(s-target)\mod 2 = 0$,我们只需要把前面 $\frac{s-target}{2}$ 这个正整数变为负数,就能使得 $s$ 与 $target$ 相等。正整数变负数的过程,实际上是将移动的方向改变,但实际移动次数仍然不变。
77+
为什么?因为如果 $s\ge \textit{target}$ 且 $(s-\textit{target})\mod 2 = 0$,我们只需要把前面 $\frac{s-\textit{target}}{2}$ 这个正整数变为负数,就能使得 $s$ 与 $\textit{target}$ 相等。正整数变负数的过程,实际上是将移动的方向改变,但实际移动次数仍然不变。
7878

79-
时间复杂度 $O(\sqrt{\left | target \right | })$,空间复杂度 $O(1)$。
79+
时间复杂度 $O(\sqrt{\left | \textit{target} \right | })$,空间复杂度 $O(1)$。
8080

8181
<!-- tabs:start -->
8282

solution/0700-0799/0754.Reach a Number/README_EN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ On the 2<sup>nd</sup> move, we step from 1 to 3 (2 steps).
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Mathematical Analysis
68+
69+
Due to symmetry, each time we can choose to move left or right, so we can take the absolute value of $\textit{target}$.
70+
71+
Define $s$ as the current position, and use the variable $k$ to record the number of moves. Initially, both $s$ and $k$ are $0$.
72+
73+
We keep adding to $s$ in a loop until $s \ge \textit{target}$ and $(s - \textit{target}) \bmod 2 = 0$. At this point, the number of moves $k$ is the answer, and we return it directly.
74+
75+
Why? Because if $s \ge \textit{target}$ and $(s - \textit{target}) \bmod 2 = 0$, we only need to change the sign of the positive integer $\frac{s - \textit{target}}{2}$ to negative, so that $s$ equals $\textit{target}$. Changing the sign of a positive integer essentially means changing the direction of the move, but the actual number of moves remains the same.
76+
77+
The time complexity is $O(\sqrt{\left | \textit{target} \right | })$, and the space complexity is $O(1)$.
6878

6979
<!-- tabs:start -->
7080

0 commit comments

Comments
 (0)