File tree Expand file tree Collapse file tree 5 files changed +165
-1
lines changed Expand file tree Collapse file tree 5 files changed +165
-1
lines changed Original file line number Diff line number Diff line change 46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
49
+ ** 方法一:统计入度和出度**
50
+
51
+ 我们创建两个数组 $ind$ 和 $outd$,分别用于记录每个点的入度和出度,用哈希表 $s$ 保存每个节点。
52
+
53
+ 接下来,遍历每个节点 $c$,如果存在 $ind[ c] $ 等于节点总数减去 $1$,并且 $outd[ c] =0$,说明存在满足条件的交通枢纽,返回 $c$。
54
+
55
+ 否则遍历结束,返回 $-1$。
56
+
57
+ 时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是节点数量以及路径的数量。
58
+
49
59
<!-- tabs:start -->
50
60
51
61
### ** Python3**
52
62
53
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
54
64
55
65
``` python
56
-
66
+ class Solution :
67
+ def transportationHub (self , path : List[List[int ]]) -> int :
68
+ ind = Counter()
69
+ outd = Counter()
70
+ s = set ()
71
+ for a, b in path:
72
+ s.add(a)
73
+ s.add(b)
74
+ outd[a] += 1
75
+ ind[b] += 1
76
+ for c in s:
77
+ if ind[c] == len (s) - 1 and outd[c] == 0 :
78
+ return c
79
+ return - 1
57
80
```
58
81
59
82
### ** Java**
60
83
61
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
85
63
86
``` java
87
+ class Solution {
88
+ public int transportationHub (int [][] path ) {
89
+ int [] ind = new int [1001 ];
90
+ int [] outd = new int [1001 ];
91
+ Set<Integer > s = new HashSet<> ();
92
+ for (int [] p : path) {
93
+ int a = p[0 ], b = p[1 ];
94
+ s. add(a);
95
+ s. add(b);
96
+ ind[b]++ ;
97
+ outd[a]++ ;
98
+ }
99
+ for (int c : s) {
100
+ if (ind[c] == s. size() - 1 && outd[c] == 0 ) {
101
+ return c;
102
+ }
103
+ }
104
+ return - 1 ;
105
+ }
106
+ }
107
+ ```
108
+
109
+ ### ** C++**
110
+
111
+ ``` cpp
112
+ class Solution {
113
+ public:
114
+ int transportationHub(vector<vector<int >>& path) {
115
+ int ind[ 1001] {};
116
+ int outd[ 1001] {};
117
+ unordered_set<int > s;
118
+ for (auto& p : path) {
119
+ int a = p[ 0] , b = p[ 1] ;
120
+ s.insert(a);
121
+ s.insert(b);
122
+ ind[ b] ++;
123
+ outd[ a] ++;
124
+ }
125
+ for (int c : s) {
126
+ if (ind[ c] == s.size() - 1 && outd[ c] == 0) {
127
+ return c;
128
+ }
129
+ }
130
+ return -1;
131
+ }
132
+ };
133
+ ```
64
134
135
+ ### **Go**
136
+
137
+ ```go
138
+ func transportationHub(path [][]int) int {
139
+ ind := [1001]int{}
140
+ outd := [1001]int{}
141
+ s := map[int]struct{}{}
142
+ for _, p := range path {
143
+ a, b := p[0], p[1]
144
+ s[a] = struct{}{}
145
+ s[b] = struct{}{}
146
+ outd[a]++
147
+ ind[b]++
148
+ }
149
+ for c := range s {
150
+ if ind[c] == len(s)-1 && outd[c] == 0 {
151
+ return c
152
+ }
153
+ }
154
+ return -1
155
+ }
65
156
```
66
157
67
158
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int transportationHub (vector<vector<int >>& path) {
4
+ int ind[1001 ]{};
5
+ int outd[1001 ]{};
6
+ unordered_set<int > s;
7
+ for (auto & p : path) {
8
+ int a = p[0 ], b = p[1 ];
9
+ s.insert (a);
10
+ s.insert (b);
11
+ ind[b]++;
12
+ outd[a]++;
13
+ }
14
+ for (int c : s) {
15
+ if (ind[c] == s.size () - 1 && outd[c] == 0 ) {
16
+ return c;
17
+ }
18
+ }
19
+ return -1 ;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ func transportationHub (path [][]int ) int {
2
+ ind := [1001 ]int {}
3
+ outd := [1001 ]int {}
4
+ s := map [int ]struct {}{}
5
+ for _ , p := range path {
6
+ a , b := p [0 ], p [1 ]
7
+ s [a ] = struct {}{}
8
+ s [b ] = struct {}{}
9
+ outd [a ]++
10
+ ind [b ]++
11
+ }
12
+ for c := range s {
13
+ if ind [c ] == len (s )- 1 && outd [c ] == 0 {
14
+ return c
15
+ }
16
+ }
17
+ return - 1
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int transportationHub (int [][] path ) {
3
+ int [] ind = new int [1001 ];
4
+ int [] outd = new int [1001 ];
5
+ Set <Integer > s = new HashSet <>();
6
+ for (int [] p : path ) {
7
+ int a = p [0 ], b = p [1 ];
8
+ s .add (a );
9
+ s .add (b );
10
+ ind [b ]++;
11
+ outd [a ]++;
12
+ }
13
+ for (int c : s ) {
14
+ if (ind [c ] == s .size () - 1 && outd [c ] == 0 ) {
15
+ return c ;
16
+ }
17
+ }
18
+ return -1 ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def transportationHub (self , path : List [List [int ]]) -> int :
3
+ ind = Counter ()
4
+ outd = Counter ()
5
+ s = set ()
6
+ for a , b in path :
7
+ s .add (a )
8
+ s .add (b )
9
+ outd [a ] += 1
10
+ ind [b ] += 1
11
+ for c in s :
12
+ if ind [c ] == len (s ) - 1 and outd [c ] == 0 :
13
+ return c
14
+ return - 1
You can’t perform that action at this time.
0 commit comments