Skip to content

Commit 45adcec

Browse files
authored
chore: update lc problems (#3533)
1 parent 3ae053d commit 45adcec

File tree

39 files changed

+560
-88
lines changed

39 files changed

+560
-88
lines changed

solution/0400-0499/0461.Hamming Distance/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tags:
2020

2121
<p>给你两个整数 <code>x</code> 和 <code>y</code>,计算并返回它们之间的汉明距离。</p>
2222

23-
<p> </p>
23+
<p>&nbsp;</p>
2424

2525
<p><strong>示例 1:</strong></p>
2626

@@ -41,14 +41,18 @@ tags:
4141
<strong>输出:</strong>1
4242
</pre>
4343

44-
<p> </p>
44+
<p>&nbsp;</p>
4545

4646
<p><strong>提示:</strong></p>
4747

4848
<ul>
49-
<li><code>0 <= x, y <= 2<sup>31</sup> - 1</code></li>
49+
<li><code>0 &lt;=&nbsp;x, y &lt;= 2<sup>31</sup> - 1</code></li>
5050
</ul>
5151

52+
<p>&nbsp;</p>
53+
54+
<p><strong>注意:</strong>本题与&nbsp;<a href="https://leetcode.cn/problems/minimum-bit-flips-to-convert-number/">2220. 转换数字的最少位翻转次数</a>&nbsp;相同。</p>
55+
5256
<!-- description:end -->
5357

5458
## 解法

solution/0800-0899/0815.Bus Routes/README.md

+32-31
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tags:
3535
<pre>
3636
<strong>输入:</strong>routes = [[1,2,7],[3,6,7]], source = 1, target = 6
3737
<strong>输出:</strong>2
38-
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
38+
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
3939
</pre>
4040

4141
<p><strong>示例 2:</strong></p>
@@ -367,55 +367,56 @@ function numBusesToDestination(routes: number[][], source: number, target: numbe
367367
public class Solution {
368368
public int NumBusesToDestination(int[][] routes, int source, int target) {
369369
if (source == target) {
370-
return 0;
370+
return 0; // 如果起点和终点相同,直接返回 0
371371
}
372372

373-
Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
374-
List<HashSet<int>> routeToStops = new List<HashSet<int>>();
375-
373+
// 使用 Dictionary 构建站点到公交线路的映射
374+
var g = new Dictionary<int, List<int>>();
376375
for (int i = 0; i < routes.Length; i++) {
377-
routeToStops.Add(new HashSet<int>());
378376
foreach (int stop in routes[i]) {
379-
routeToStops[i].Add(stop);
380-
if (!stopToRoutes.ContainsKey(stop)) {
381-
stopToRoutes[stop] = new HashSet<int>();
377+
if (!g.ContainsKey(stop)) {
378+
g[stop] = new List<int>();
382379
}
383-
stopToRoutes[stop].Add(i);
380+
g[stop].Add(i); // 将公交线路编号添加到该站点的列表中
384381
}
385382
}
386383

387-
Queue<int> queue = new Queue<int>();
388-
HashSet<int> visited = new HashSet<int>();
389-
int ans = 0;
390-
391-
foreach (int routeId in stopToRoutes[source]) {
392-
queue.Enqueue(routeId);
393-
visited.Add(routeId);
384+
// 如果 source 或 target 不在站点映射中,返回 -1
385+
if (!g.ContainsKey(source) || !g.ContainsKey(target)) {
386+
return -1;
394387
}
395388

396-
while (queue.Count > 0) {
397-
int count = queue.Count;
398-
ans++;
389+
// 初始化队列和访问集合
390+
var q = new Queue<int[]>();
391+
var visBus = new HashSet<int>(); // 记录访问过的公交线路
392+
var visStop = new HashSet<int>(); // 记录访问过的站点
393+
q.Enqueue(new int[] { source, 0 }); // 将起点加入队列,公交次数初始化为 0
394+
visStop.Add(source); // 将起点标记为已访问
399395
400-
for (int i = 0; i < count; i++) {
401-
int routeId = queue.Dequeue();
396+
// 开始广度优先搜索
397+
while (q.Count > 0) {
398+
var current = q.Dequeue(); // 从队列中取出当前站点
399+
int stop = current[0], busCount = current[1];
402400

403-
foreach (int stop in routeToStops[routeId]) {
404-
if (stop == target) {
405-
return ans;
406-
}
401+
// 如果当前站点是目标站点,返回所需的公交次数
402+
if (stop == target) {
403+
return busCount;
404+
}
407405

408-
foreach (int nextRoute in stopToRoutes[stop]) {
409-
if (!visited.Contains(nextRoute)) {
410-
visited.Add(nextRoute);
411-
queue.Enqueue(nextRoute);
406+
// 遍历经过当前站点的所有公交线路
407+
foreach (int bus in g[stop]) {
408+
if (visBus.Add(bus)) { // 如果公交线路没有被访问过
409+
// 遍历该线路上的所有站点
410+
foreach (int nextStop in routes[bus]) {
411+
if (visStop.Add(nextStop)) { // 如果该站点没有被访问过
412+
q.Enqueue(new int[] { nextStop, busCount + 1 }); // 将新站点加入队列,公交次数加 1
412413
}
413414
}
414415
}
415416
}
416417
}
417418

418-
return -1;
419+
return -1; // 如果无法到达目标站点,返回 -1
419420
}
420421
}
421422
```

solution/0800-0899/0815.Bus Routes/README_EN.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ public class Solution {
327327
return 0;
328328
}
329329

330-
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
330+
// Use Dictionary to map stops to bus routes
331+
var g = new Dictionary<int, List<int>>();
331332
for (int i = 0; i < routes.Length; i++) {
332333
foreach (int stop in routes[i]) {
333334
if (!g.ContainsKey(stop)) {
@@ -337,36 +338,42 @@ public class Solution {
337338
}
338339
}
339340

341+
// If source or target is not in the mapping, return -1
340342
if (!g.ContainsKey(source) || !g.ContainsKey(target)) {
341343
return -1;
342344
}
343345

344-
Queue<int[]> q = new Queue<int[]>();
345-
HashSet<int> visBus = new HashSet<int>();
346-
HashSet<int> visStop = new HashSet<int>();
347-
q.Enqueue(new int[]{source, 0});
346+
// Initialize queue and visited sets
347+
var q = new Queue<int[]>();
348+
var visBus = new HashSet<int>();
349+
var visStop = new HashSet<int>();
350+
q.Enqueue(new int[] { source, 0 });
348351
visStop.Add(source);
349352

353+
// Begin BFS
350354
while (q.Count > 0) {
351-
int[] current = q.Dequeue();
355+
var current = q.Dequeue();
352356
int stop = current[0], busCount = current[1];
357+
358+
// If the current stop is the target stop, return the bus count
353359
if (stop == target) {
354360
return busCount;
355361
}
362+
363+
// Traverse all bus routes passing through the current stop
356364
foreach (int bus in g[stop]) {
357-
if (!visBus.Contains(bus)) {
365+
if (visBus.Add(bus)) {
366+
// Traverse all stops on this bus route
358367
foreach (int nextStop in routes[bus]) {
359-
if (!visStop.Contains(nextStop)) {
360-
visBus.Add(bus);
361-
visStop.Add(nextStop);
362-
q.Enqueue(new int[]{nextStop, busCount + 1});
368+
if (visStop.Add(nextStop)) {
369+
q.Enqueue(new int[] { nextStop, busCount + 1 });
363370
}
364371
}
365372
}
366373
}
367374
}
368375

369-
return -1;
376+
return -1; // If unable to reach the target stop, return -1
370377
}
371378
}
372379
```

solution/0800-0899/0815.Bus Routes/Solution.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public int NumBusesToDestination(int[][] routes, int source, int target) {
44
return 0;
55
}
66

7-
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
7+
// Use Dictionary to map stops to bus routes
8+
var g = new Dictionary<int, List<int>>();
89
for (int i = 0; i < routes.Length; i++) {
910
foreach (int stop in routes[i]) {
1011
if (!g.ContainsKey(stop)) {
@@ -14,35 +15,41 @@ public int NumBusesToDestination(int[][] routes, int source, int target) {
1415
}
1516
}
1617

18+
// If source or target is not in the mapping, return -1
1719
if (!g.ContainsKey(source) || !g.ContainsKey(target)) {
1820
return -1;
1921
}
2022

21-
Queue<int[]> q = new Queue<int[]>();
22-
HashSet<int> visBus = new HashSet<int>();
23-
HashSet<int> visStop = new HashSet<int>();
24-
q.Enqueue(new int[]{source, 0});
23+
// Initialize queue and visited sets
24+
var q = new Queue<int[]>();
25+
var visBus = new HashSet<int>();
26+
var visStop = new HashSet<int>();
27+
q.Enqueue(new int[] { source, 0 });
2528
visStop.Add(source);
2629

30+
// Begin BFS
2731
while (q.Count > 0) {
28-
int[] current = q.Dequeue();
32+
var current = q.Dequeue();
2933
int stop = current[0], busCount = current[1];
34+
35+
// If the current stop is the target stop, return the bus count
3036
if (stop == target) {
3137
return busCount;
3238
}
39+
40+
// Traverse all bus routes passing through the current stop
3341
foreach (int bus in g[stop]) {
34-
if (!visBus.Contains(bus)) {
42+
if (visBus.Add(bus)) {
43+
// Traverse all stops on this bus route
3544
foreach (int nextStop in routes[bus]) {
36-
if (!visStop.Contains(nextStop)) {
37-
visBus.Add(bus);
38-
visStop.Add(nextStop);
39-
q.Enqueue(new int[]{nextStop, busCount + 1});
45+
if (visStop.Add(nextStop)) {
46+
q.Enqueue(new int[] { nextStop, busCount + 1 });
4047
}
4148
}
4249
}
4350
}
4451
}
4552

46-
return -1;
53+
return -1; // If unable to reach the target stop, return -1
4754
}
4855
}

solution/2200-2299/2220.Minimum Bit Flips to Convert Number/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ tags:
5959
<li><code>0 &lt;= start, goal &lt;= 10<sup>9</sup></code></li>
6060
</ul>
6161

62+
<p>&nbsp;</p>
63+
64+
<p><strong>注意:</strong>本题与&nbsp;<a href="https://leetcode.cn/problems/hamming-distance/">461. 汉明距离</a>&nbsp;相同。</p>
65+
6266
<!-- description:end -->
6367

6468
## 解法

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- 二分查找
1111
- 前缀和
1212
- 滑动窗口
13+
- 单调队列
1314
- 堆(优先队列)
1415
---
1516

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- Binary Search
1111
- Prefix Sum
1212
- Sliding Window
13+
- Monotonic Queue
1314
- Heap (Priority Queue)
1415
---
1516

solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ tags:
5252
<strong>输入:</strong>nums = [1,2,3,4]
5353
<strong>输出:</strong>1
5454
<strong>解释:</strong>
55-
子数组按位与运算的最大值是 4 。
55+
子数组按位与运算的最大值是 4 。
5656
能得到此结果的最长子数组是 [4],所以返回 1 。
5757
</pre>
5858

solution/2800-2899/2868.The Wording Game/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/2800-2899/2868.The%20Wording%20Game/README.md
55
tags:
6+
- 贪心
67
- 数组
78
- 数学
89
- 双指针

solution/2800-2899/2868.The Wording Game/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/2800-2899/2868.The%20Wording%20Game/README_EN.md
55
tags:
6+
- Greedy
67
- Array
78
- Math
89
- Two Pointers

solution/3000-3099/3088.Make String Anti-palindrome/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3088.Ma
55
tags:
66
- 贪心
77
- 字符串
8+
- 计数排序
89
- 排序
910
---
1011

solution/3000-3099/3088.Make String Anti-palindrome/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3088.Ma
55
tags:
66
- Greedy
77
- String
8+
- Counting Sort
89
- Sorting
910
---
1011

solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README.md
5+
tags:
6+
- 数组
7+
- 双指针
8+
- 动态规划
59
---
610

711
<!-- problem:start -->

solution/3200-3299/3284.Sum of Consecutive Subarrays/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README_EN.md
5+
tags:
6+
- Array
7+
- Two Pointers
8+
- Dynamic Programming
59
---
610

711
<!-- problem:start -->

solution/3200-3299/3285.Find Indices of Stable Mountains/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3285.Find%20Indices%20of%20Stable%20Mountains/README.md
5+
tags:
6+
- 数组
57
---
68

79
<!-- problem:start -->

solution/3200-3299/3285.Find Indices of Stable Mountains/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3285.Find%20Indices%20of%20Stable%20Mountains/README_EN.md
5+
tags:
6+
- Array
57
---
68

79
<!-- problem:start -->

solution/3200-3299/3286.Find a Safe Walk Through a Grid/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3286.Find%20a%20Safe%20Walk%20Through%20a%20Grid/README.md
5+
tags:
6+
- 广度优先搜索
7+
-
8+
- 数组
9+
- 矩阵
10+
- 最短路
11+
- 堆(优先队列)
512
---
613

714
<!-- problem:start -->

solution/3200-3299/3286.Find a Safe Walk Through a Grid/README_EN.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3286.Find%20a%20Safe%20Walk%20Through%20a%20Grid/README_EN.md
5+
tags:
6+
- Breadth-First Search
7+
- Graph
8+
- Array
9+
- Matrix
10+
- Shortest Path
11+
- Heap (Priority Queue)
512
---
613

714
<!-- problem:start -->

solution/3200-3299/3287.Find the Maximum Sequence Value of Array/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3287.Find%20the%20Maximum%20Sequence%20Value%20of%20Array/README.md
5+
tags:
6+
- 位运算
7+
- 数组
8+
- 动态规划
59
---
610

711
<!-- problem:start -->

0 commit comments

Comments
 (0)