Skip to content

chore: update lc problems #3533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions solution/0400-0499/0461.Hamming Distance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

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

<p> </p>
<p>&nbsp;</p>

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

Expand All @@ -41,14 +41,18 @@ tags:
<strong>输出:</strong>1
</pre>

<p> </p>
<p>&nbsp;</p>

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

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

<p>&nbsp;</p>

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

<!-- description:end -->

## 解法
Expand Down
63 changes: 32 additions & 31 deletions solution/0800-0899/0815.Bus Routes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tags:
<pre>
<strong>输入:</strong>routes = [[1,2,7],[3,6,7]], source = 1, target = 6
<strong>输出:</strong>2
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
</pre>

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

Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
List<HashSet<int>> routeToStops = new List<HashSet<int>>();

// 使用 Dictionary 构建站点到公交线路的映射
var g = new Dictionary<int, List<int>>();
for (int i = 0; i < routes.Length; i++) {
routeToStops.Add(new HashSet<int>());
foreach (int stop in routes[i]) {
routeToStops[i].Add(stop);
if (!stopToRoutes.ContainsKey(stop)) {
stopToRoutes[stop] = new HashSet<int>();
if (!g.ContainsKey(stop)) {
g[stop] = new List<int>();
}
stopToRoutes[stop].Add(i);
g[stop].Add(i); // 将公交线路编号添加到该站点的列表中
}
}

Queue<int> queue = new Queue<int>();
HashSet<int> visited = new HashSet<int>();
int ans = 0;

foreach (int routeId in stopToRoutes[source]) {
queue.Enqueue(routeId);
visited.Add(routeId);
// 如果 source 或 target 不在站点映射中,返回 -1
if (!g.ContainsKey(source) || !g.ContainsKey(target)) {
return -1;
}

while (queue.Count > 0) {
int count = queue.Count;
ans++;
// 初始化队列和访问集合
var q = new Queue<int[]>();
var visBus = new HashSet<int>(); // 记录访问过的公交线路
var visStop = new HashSet<int>(); // 记录访问过的站点
q.Enqueue(new int[] { source, 0 }); // 将起点加入队列,公交次数初始化为 0
visStop.Add(source); // 将起点标记为已访问

for (int i = 0; i < count; i++) {
int routeId = queue.Dequeue();
// 开始广度优先搜索
while (q.Count > 0) {
var current = q.Dequeue(); // 从队列中取出当前站点
int stop = current[0], busCount = current[1];

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

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

return -1;
return -1; // 如果无法到达目标站点,返回 -1
}
}
```
Expand Down
31 changes: 19 additions & 12 deletions solution/0800-0899/0815.Bus Routes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ public class Solution {
return 0;
}

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

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

Queue<int[]> q = new Queue<int[]>();
HashSet<int> visBus = new HashSet<int>();
HashSet<int> visStop = new HashSet<int>();
q.Enqueue(new int[]{source, 0});
// Initialize queue and visited sets
var q = new Queue<int[]>();
var visBus = new HashSet<int>();
var visStop = new HashSet<int>();
q.Enqueue(new int[] { source, 0 });
visStop.Add(source);

// Begin BFS
while (q.Count > 0) {
int[] current = q.Dequeue();
var current = q.Dequeue();
int stop = current[0], busCount = current[1];

// If the current stop is the target stop, return the bus count
if (stop == target) {
return busCount;
}

// Traverse all bus routes passing through the current stop
foreach (int bus in g[stop]) {
if (!visBus.Contains(bus)) {
if (visBus.Add(bus)) {
// Traverse all stops on this bus route
foreach (int nextStop in routes[bus]) {
if (!visStop.Contains(nextStop)) {
visBus.Add(bus);
visStop.Add(nextStop);
q.Enqueue(new int[]{nextStop, busCount + 1});
if (visStop.Add(nextStop)) {
q.Enqueue(new int[] { nextStop, busCount + 1 });
}
}
}
}
}

return -1;
return -1; // If unable to reach the target stop, return -1
}
}
```
Expand Down
31 changes: 19 additions & 12 deletions solution/0800-0899/0815.Bus Routes/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public int NumBusesToDestination(int[][] routes, int source, int target) {
return 0;
}

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

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

Queue<int[]> q = new Queue<int[]>();
HashSet<int> visBus = new HashSet<int>();
HashSet<int> visStop = new HashSet<int>();
q.Enqueue(new int[]{source, 0});
// Initialize queue and visited sets
var q = new Queue<int[]>();
var visBus = new HashSet<int>();
var visStop = new HashSet<int>();
q.Enqueue(new int[] { source, 0 });
visStop.Add(source);

// Begin BFS
while (q.Count > 0) {
int[] current = q.Dequeue();
var current = q.Dequeue();
int stop = current[0], busCount = current[1];

// If the current stop is the target stop, return the bus count
if (stop == target) {
return busCount;
}

// Traverse all bus routes passing through the current stop
foreach (int bus in g[stop]) {
if (!visBus.Contains(bus)) {
if (visBus.Add(bus)) {
// Traverse all stops on this bus route
foreach (int nextStop in routes[bus]) {
if (!visStop.Contains(nextStop)) {
visBus.Add(bus);
visStop.Add(nextStop);
q.Enqueue(new int[]{nextStop, busCount + 1});
if (visStop.Add(nextStop)) {
q.Enqueue(new int[] { nextStop, busCount + 1 });
}
}
}
}
}

return -1;
return -1; // If unable to reach the target stop, return -1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ tags:
<li><code>0 &lt;= start, goal &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>

<p><strong>注意:</strong>本题与&nbsp;<a href="https://leetcode.cn/problems/hamming-distance/">461. 汉明距离</a>&nbsp;相同。</p>

<!-- description:end -->

## 解法
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tags:
- 二分查找
- 前缀和
- 滑动窗口
- 单调队列
- 堆(优先队列)
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tags:
- Binary Search
- Prefix Sum
- Sliding Window
- Monotonic Queue
- Heap (Priority Queue)
---

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

Expand Down
1 change: 1 addition & 0 deletions solution/2800-2899/2868.The Wording Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/2800-2899/2868.The%20Wording%20Game/README.md
tags:
- 贪心
- 数组
- 数学
- 双指针
Expand Down
1 change: 1 addition & 0 deletions solution/2800-2899/2868.The Wording Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/2800-2899/2868.The%20Wording%20Game/README_EN.md
tags:
- Greedy
- Array
- Math
- Two Pointers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3088.Ma
tags:
- 贪心
- 字符串
- 计数排序
- 排序
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3088.Ma
tags:
- Greedy
- String
- Counting Sort
- Sorting
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README.md
tags:
- 数组
- 双指针
- 动态规划
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README_EN.md
tags:
- Array
- Two Pointers
- Dynamic Programming
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3285.Find%20Indices%20of%20Stable%20Mountains/README.md
tags:
- 数组
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3285.Find%20Indices%20of%20Stable%20Mountains/README_EN.md
tags:
- Array
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3286.Find%20a%20Safe%20Walk%20Through%20a%20Grid/README.md
tags:
- 广度优先搜索
- 图
- 数组
- 矩阵
- 最短路
- 堆(优先队列)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3286.Find%20a%20Safe%20Walk%20Through%20a%20Grid/README_EN.md
tags:
- Breadth-First Search
- Graph
- Array
- Matrix
- Shortest Path
- Heap (Priority Queue)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3287.Find%20the%20Maximum%20Sequence%20Value%20of%20Array/README.md
tags:
- 位运算
- 数组
- 动态规划
---

<!-- problem:start -->
Expand Down
Loading
Loading