Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b84f6c5

Browse files
feat: add cs solution to lc problem: No.815 (doocs#1957)
1 parent f5f8de8 commit b84f6c5

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,65 @@ func numBusesToDestination(routes [][]int, source int, target int) int {
281281
}
282282
```
283283

284+
### **C#**
285+
286+
```cs
287+
public class Solution {
288+
public int NumBusesToDestination(int[][] routes, int source, int target) {
289+
if (source == target) {
290+
return 0;
291+
}
292+
293+
Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
294+
List<HashSet<int>> routeToStops = new List<HashSet<int>>();
295+
296+
for (int i = 0; i < routes.Length; i++) {
297+
routeToStops.Add(new HashSet<int>());
298+
foreach (int stop in routes[i]) {
299+
routeToStops[i].Add(stop);
300+
if (!stopToRoutes.ContainsKey(stop)) {
301+
stopToRoutes[stop] = new HashSet<int>();
302+
}
303+
stopToRoutes[stop].Add(i);
304+
}
305+
}
306+
307+
Queue<int> queue = new Queue<int>();
308+
HashSet<int> visited = new HashSet<int>();
309+
int ans = 0;
310+
311+
foreach (int routeId in stopToRoutes[source]) {
312+
queue.Enqueue(routeId);
313+
visited.Add(routeId);
314+
}
315+
316+
while (queue.Count > 0) {
317+
int count = queue.Count;
318+
ans++;
319+
320+
for (int i = 0; i < count; i++) {
321+
int routeId = queue.Dequeue();
322+
323+
foreach (int stop in routeToStops[routeId]) {
324+
if (stop == target) {
325+
return ans;
326+
}
327+
328+
foreach (int nextRoute in stopToRoutes[stop]) {
329+
if (!visited.Contains(nextRoute)) {
330+
visited.Add(nextRoute);
331+
queue.Enqueue(nextRoute);
332+
}
333+
}
334+
}
335+
}
336+
}
337+
338+
return -1;
339+
}
340+
}
341+
```
342+
284343
### **...**
285344

286345
```

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,65 @@ func numBusesToDestination(routes [][]int, source int, target int) int {
256256
}
257257
```
258258

259+
### **C#**
260+
261+
```cs
262+
public class Solution {
263+
public int NumBusesToDestination(int[][] routes, int source, int target) {
264+
if (source == target) {
265+
return 0;
266+
}
267+
268+
Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
269+
List<HashSet<int>> routeToStops = new List<HashSet<int>>();
270+
271+
for (int i = 0; i < routes.Length; i++) {
272+
routeToStops.Add(new HashSet<int>());
273+
foreach (int stop in routes[i]) {
274+
routeToStops[i].Add(stop);
275+
if (!stopToRoutes.ContainsKey(stop)) {
276+
stopToRoutes[stop] = new HashSet<int>();
277+
}
278+
stopToRoutes[stop].Add(i);
279+
}
280+
}
281+
282+
Queue<int> queue = new Queue<int>();
283+
HashSet<int> visited = new HashSet<int>();
284+
int ans = 0;
285+
286+
foreach (int routeId in stopToRoutes[source]) {
287+
queue.Enqueue(routeId);
288+
visited.Add(routeId);
289+
}
290+
291+
while (queue.Count > 0) {
292+
int count = queue.Count;
293+
ans++;
294+
295+
for (int i = 0; i < count; i++) {
296+
int routeId = queue.Dequeue();
297+
298+
foreach (int stop in routeToStops[routeId]) {
299+
if (stop == target) {
300+
return ans;
301+
}
302+
303+
foreach (int nextRoute in stopToRoutes[stop]) {
304+
if (!visited.Contains(nextRoute)) {
305+
visited.Add(nextRoute);
306+
queue.Enqueue(nextRoute);
307+
}
308+
}
309+
}
310+
}
311+
}
312+
313+
return -1;
314+
}
315+
}
316+
```
317+
259318
### **...**
260319

261320
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Solution {
2+
public int NumBusesToDestination(int[][] routes, int source, int target) {
3+
if (source == target) {
4+
return 0;
5+
}
6+
7+
Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
8+
List<HashSet<int>> routeToStops = new List<HashSet<int>>();
9+
10+
for (int i = 0; i < routes.Length; i++) {
11+
routeToStops.Add(new HashSet<int>());
12+
foreach (int stop in routes[i]) {
13+
routeToStops[i].Add(stop);
14+
if (!stopToRoutes.ContainsKey(stop)) {
15+
stopToRoutes[stop] = new HashSet<int>();
16+
}
17+
stopToRoutes[stop].Add(i);
18+
}
19+
}
20+
21+
Queue<int> queue = new Queue<int>();
22+
HashSet<int> visited = new HashSet<int>();
23+
int ans = 0;
24+
25+
foreach (int routeId in stopToRoutes[source]) {
26+
queue.Enqueue(routeId);
27+
visited.Add(routeId);
28+
}
29+
30+
while (queue.Count > 0) {
31+
int count = queue.Count;
32+
ans++;
33+
34+
for (int i = 0; i < count; i++) {
35+
int routeId = queue.Dequeue();
36+
37+
foreach (int stop in routeToStops[routeId]) {
38+
if (stop == target) {
39+
return ans;
40+
}
41+
42+
foreach (int nextRoute in stopToRoutes[stop]) {
43+
if (!visited.Contains(nextRoute)) {
44+
visited.Add(nextRoute);
45+
queue.Enqueue(nextRoute);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
return -1;
53+
}
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /