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 fc77802

Browse files
committed
feat: add solutions to lc problem: No.0997
No.0997.Find the Town Judge
1 parent 31da159 commit fc77802

File tree

9 files changed

+201
-60
lines changed

9 files changed

+201
-60
lines changed

‎solution/0900-0999/0997.Find the Town Judge/README.md‎

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@
6363
<li><code>1 &lt;= trust[i][0], trust[i][1] &lt;= N</code></li>
6464
</ol>
6565

66-
6766
## 解法
6867

6968
<!-- 这里可写通用的实现逻辑 -->
7069

70+
创建长度为 1001 的计数器 `c1[i]``c2[i]`,其中 `c1[i]` 表示 i 信任的人数,`c2[i]` 表示信任 i 的人数。
71+
72+
遍历 trust 列表,统计人数,最后再遍历计数器,若存在 `c1[i] == 0 && c2[i] == n - 1`,说明存在法官,直接返回 i。否则遍历结束返回 -1。
73+
7174
<!-- tabs:start -->
7275

7376
### **Python3**
@@ -77,28 +80,39 @@
7780
```python
7881
class Solution:
7982
def findJudge(self, n: int, trust: List[List[int]]) -> int:
80-
if n == 1 and len(trust) == 0:
81-
return 1
82-
dic = {}
83-
values = set()
84-
for i in trust:
85-
values.add(i[0])
86-
if i[1] in dic:
87-
dic[i[1]].append(i[0])
88-
else:
89-
dic[i[1]] = [i[0]]
90-
91-
for key, value in dic.items():
92-
if len(dic[key]) == n-1 and key not in values:
93-
return k
83+
N = 1001
84+
c1, c2 = [0] * N, [0] * N
85+
for a, b in trust:
86+
c1[a] += 1
87+
c2[b] += 1
88+
for i in range(1, N):
89+
if c1[i] == 0 and c2[i] == n - 1:
90+
return i
91+
return -1
9492
```
9593

9694
### **Java**
9795

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

10098
```java
101-
99+
class Solution {
100+
public int findJudge(int n, int[][] trust) {
101+
int N = 1001;
102+
int[] c1 = new int[N];
103+
int[] c2 = new int[N];
104+
for (int[] e : trust) {
105+
++c1[e[0]];
106+
++c2[e[1]];
107+
}
108+
for (int i = 1; i < N; ++i) {
109+
if (c1[i] == 0 && c2[i] == n - 1) {
110+
return i;
111+
}
112+
}
113+
return -1;
114+
}
115+
}
102116
```
103117

104118
### **TypeScript**
@@ -122,6 +136,49 @@ function findJudge(n: number, trust: number[][]): number {
122136
};
123137
```
124138

139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
int findJudge(int n, vector<vector<int>>& trust) {
145+
int N = 1001;
146+
vector<int> c1(N);
147+
vector<int> c2(N);
148+
for (auto& e : trust)
149+
{
150+
++c1[e[0]];
151+
++c2[e[1]];
152+
}
153+
for (int i = 1; i < N; ++i)
154+
{
155+
if (c1[i] == 0 && c2[i] == n - 1) return i;
156+
}
157+
return -1;
158+
}
159+
};
160+
```
161+
162+
### **Go**
163+
164+
```go
165+
func findJudge(n int, trust [][]int) int {
166+
N := 1001
167+
c1 := make([]int, N)
168+
c2 := make([]int, N)
169+
for _, e := range trust {
170+
c1[e[0]]++
171+
c2[e[1]]++
172+
}
173+
for i := 1; i < N; i++ {
174+
if c1[i] == 0 && c2[i] == n-1 {
175+
return i
176+
}
177+
}
178+
return -1
179+
}
180+
```
181+
125182
### **...**
126183

127184
```

‎solution/0900-0999/0997.Find the Town Judge/README_EN.md‎

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,18 @@
66

77
<p>In a town, there are <code>N</code> people labelled from&nbsp;<code>1</code> to <code>N</code>.&nbsp; There is a rumor that one of these people is secretly the town judge.</p>
88

9-
10-
119
<p>If the&nbsp;town judge exists, then:</p>
1210

13-
14-
1511
<ol>
1612
<li>The town judge trusts nobody.</li>
1713
<li>Everybody (except for the town judge) trusts the town judge.</li>
1814
<li>There is exactly one person that satisfies properties 1 and 2.</li>
1915
</ol>
2016

21-
22-
2317
<p>You are given <code>trust</code>, an array of pairs <code>trust[i] = [a, b]</code> representing that the person labelled <code>a</code> trusts the person labelled <code>b</code>.</p>
2418

25-
26-
2719
<p>If the town judge exists and can be identified, return the label of the town judge.&nbsp; Otherwise, return <code>-1</code>.</p>
2820

29-
30-
3121
<p>&nbsp;</p>
3222

3323
<p><strong>Example 1:</strong></p>
@@ -66,8 +56,6 @@
6656

6757
<p><strong>Constraints:</strong></p>
6858

69-
70-
7159
<ul>
7260
<li><code>1 &lt;= N &lt;= 1000</code></li>
7361
<li><code>0 &lt;= trust.length &lt;= 10^4</code></li>
@@ -86,26 +74,37 @@
8674
```python
8775
class Solution:
8876
def findJudge(self, n: int, trust: List[List[int]]) -> int:
89-
if n == 1 and len(trust) == 0:
90-
return 1
91-
dic = {}
92-
values = set()
93-
for i in trust:
94-
values.add(i[0])
95-
if i[1] in dic:
96-
dic[i[1]].append(i[0])
97-
else:
98-
dic[i[1]] = [i[0]]
99-
100-
for key, value in dic.items():
101-
if len(dic[key]) == n-1 and key not in values:
102-
return k
77+
N = 1001
78+
c1, c2 = [0] * N, [0] * N
79+
for a, b in trust:
80+
c1[a] += 1
81+
c2[b] += 1
82+
for i in range(1, N):
83+
if c1[i] == 0 and c2[i] == n - 1:
84+
return i
85+
return -1
10386
```
10487

10588
### **Java**
10689

10790
```java
108-
91+
class Solution {
92+
public int findJudge(int n, int[][] trust) {
93+
int N = 1001;
94+
int[] c1 = new int[N];
95+
int[] c2 = new int[N];
96+
for (int[] e : trust) {
97+
++c1[e[0]];
98+
++c2[e[1]];
99+
}
100+
for (int i = 1; i < N; ++i) {
101+
if (c1[i] == 0 && c2[i] == n - 1) {
102+
return i;
103+
}
104+
}
105+
return -1;
106+
}
107+
}
109108
```
110109

111110
### **TypeScript**
@@ -129,6 +128,49 @@ function findJudge(n: number, trust: number[][]): number {
129128
};
130129
```
131130

131+
### **C++**
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
int findJudge(int n, vector<vector<int>>& trust) {
137+
int N = 1001;
138+
vector<int> c1(N);
139+
vector<int> c2(N);
140+
for (auto& e : trust)
141+
{
142+
++c1[e[0]];
143+
++c2[e[1]];
144+
}
145+
for (int i = 1; i < N; ++i)
146+
{
147+
if (c1[i] == 0 && c2[i] == n - 1) return i;
148+
}
149+
return -1;
150+
}
151+
};
152+
```
153+
154+
### **Go**
155+
156+
```go
157+
func findJudge(n int, trust [][]int) int {
158+
N := 1001
159+
c1 := make([]int, N)
160+
c2 := make([]int, N)
161+
for _, e := range trust {
162+
c1[e[0]]++
163+
c2[e[1]]++
164+
}
165+
for i := 1; i < N; i++ {
166+
if c1[i] == 0 && c2[i] == n-1 {
167+
return i
168+
}
169+
}
170+
return -1
171+
}
172+
```
173+
132174
### **...**
133175

134176
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findJudge(int n, vector<vector<int>>& trust) {
4+
int N = 1001;
5+
vector<int> c1(N);
6+
vector<int> c2(N);
7+
for (auto& e : trust)
8+
{
9+
++c1[e[0]];
10+
++c2[e[1]];
11+
}
12+
for (int i = 1; i < N; ++i)
13+
{
14+
if (c1[i] == 0 && c2[i] == n - 1) return i;
15+
}
16+
return -1;
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func findJudge(n int, trust [][]int) int {
2+
N := 1001
3+
c1 := make([]int, N)
4+
c2 := make([]int, N)
5+
for _, e := range trust {
6+
c1[e[0]]++
7+
c2[e[1]]++
8+
}
9+
for i := 1; i < N; i++ {
10+
if c1[i] == 0 && c2[i] == n-1 {
11+
return i
12+
}
13+
}
14+
return -1
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findJudge(int n, int[][] trust) {
3+
int N = 1001;
4+
int[] c1 = new int[N];
5+
int[] c2 = new int[N];
6+
for (int[] e : trust) {
7+
++c1[e[0]];
8+
++c2[e[1]];
9+
}
10+
for (int i = 1; i < N; ++i) {
11+
if (c1[i] == 0 && c2[i] == n - 1) {
12+
return i;
13+
}
14+
}
15+
return -1;
16+
}
17+
}
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
class Solution:
22
def findJudge(self, n: int, trust: List[List[int]]) -> int:
3-
if n == 1 and len(trust) == 0:
4-
return 1
5-
dic = {}
6-
values = set()
7-
for i in trust:
8-
values.add(i[0])
9-
if i[1] in dic:
10-
dic[i[1]].append(i[0])
11-
else:
12-
dic[i[1]] = [i[0]]
13-
14-
for key, value in dic.items():
15-
if len(dic[key]) == n-1 and key not in values:
16-
return k
3+
N = 1001
4+
c1, c2 = [0] * N, [0] * N
5+
for a, b in trust:
6+
c1[a] += 1
7+
c2[b] += 1
8+
for i in range(1, N):
9+
if c1[i] == 0 and c2[i] == n - 1:
10+
return i
11+
return -1

‎solution/1000-1099/1087.Brace Expansion/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class Solution:
6464
convert(s[j + 1:])
6565
else:
6666
j = s.find('{')
67-
6867
if j != -1:
6968
items.append(s[: j].split(','))
7069
convert(s[j:])

‎solution/1000-1099/1087.Brace Expansion/README_EN.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class Solution:
5252
convert(s[j + 1:])
5353
else:
5454
j = s.find('{')
55-
5655
if j != -1:
5756
items.append(s[: j].split(','))
5857
convert(s[j:])

‎solution/1000-1099/1087.Brace Expansion/Solution.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def convert(s):
99
convert(s[j + 1:])
1010
else:
1111
j = s.find('{')
12-
1312
if j != -1:
1413
items.append(s[: j].split(','))
1514
convert(s[j:])

0 commit comments

Comments
(0)

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