Skip to content

Commit 096da87

Browse files
committed
feat: add solutions to lcp problem: No.62
1 parent 655ede2 commit 096da87

File tree

5 files changed

+165
-1
lines changed

5 files changed

+165
-1
lines changed

lcp/LCP 62. 交通枢纽/README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,113 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

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+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

5363
<!-- 这里可写当前语言的特殊实现逻辑 -->
5464

5565
```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
5780
```
5881

5982
### **Java**
6083

6184
<!-- 这里可写当前语言的特殊实现逻辑 -->
6285

6386
```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+
```
64134
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+
}
65156
```
66157

67158
### **...**

lcp/LCP 62. 交通枢纽/Solution.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
};

lcp/LCP 62. 交通枢纽/Solution.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

lcp/LCP 62. 交通枢纽/Solution.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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

0 commit comments

Comments
 (0)