-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain_Station.java
More file actions
77 lines (65 loc) · 1.73 KB
/
Copy pathTrain_Station.java
File metadata and controls
77 lines (65 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class Train_Station {
private int id;
private int[] lines;
private int count;
public Train_Station(int id, int[] lines, int count) {
this.id = id;
this.lines = lines;
this.count = count;
}
public boolean isPassingThrough(int lineNum) {
for (int i = 0; i < count; i++) {
if (lines[i] == lineNum) {
return true;
}
}
return false;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int[] getLines() {
return lines;
}
public void setLines(int[] lines) {
this.lines = lines;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
/**
* Returns the set of line numbers that stop at every station in the network.
*
* @param stations All stations in the network (may be empty)
* @return an array of line numbers that appear in every station (each line appears once)
*/
public static int[] allStops(Train_Station[] stations) {
if (stations == null || stations.length == 0) {
return new int[0];
}
java.util.Set<Integer> commonLines = new java.util.HashSet<>();
for (int i = 0; i < stations[0].count; i++) {
commonLines.add(stations[0].lines[i]);
}
for (int s = 1; s < stations.length && !commonLines.isEmpty(); s++) {
Train_Station st = stations[s];
java.util.Set<Integer> stationLines = new java.util.HashSet<>();
for (int i = 0; i < st.count; i++) {
stationLines.add(st.lines[i]);
}
commonLines.retainAll(stationLines);
}
int[] result = new int[commonLines.size()];
int idx = 0;
for (int line : commonLines) {
result[idx++] = line;
}
return result;
}
}