Skip to content

Commit cd6b160

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

File tree

7 files changed

+150
-144
lines changed

7 files changed

+150
-144
lines changed

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

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63-
创建长度为 1001 的计数器 `c1[i]``c2[i]`,其中 `c1[i]` 表示 i 信任的人数,`c2[i]` 表示信任 i 的人数。
63+
**方法一:计数**
6464

65-
遍历 trust 列表,统计人数,最后再遍历计数器,若存在 `c1[i] == 0 && c2[i] == n - 1`,说明存在法官,直接返回 i。否则遍历结束返回 -1。
65+
我们创建两个长度为 $n + 1$ 的数组 $cnt1$ 和 $cnt2$,分别表示每个人信任的人数和被信任的人数。
66+
67+
接下来,我们遍历数组 $trust$,对于每一项 $[a_i, b_i]$,我们将 $cnt1[a_i]$ 和 $cnt2[b_i]$ 分别加 $1$。
68+
69+
最后,我们在 $[1,..n]$ 范围内枚举每个人 $i$,如果 $cnt1[i] = 0$ 且 $cnt2[i] = n - 1$,则说明 $i$ 是小镇法官,返回 $i$ 即可。否则遍历结束后,返回 $-1$。
70+
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $trust$ 的长度。
6672

6773
<!-- tabs:start -->
6874

@@ -73,13 +79,13 @@
7379
```python
7480
class Solution:
7581
def findJudge(self, n: int, trust: List[List[int]]) -> int:
76-
N = 1001
77-
c1, c2 = [0] * N, [0] * N
82+
cnt1 = [0] * (n + 1)
83+
cnt2 = [0] * (n + 1)
7884
for a, b in trust:
79-
c1[a] += 1
80-
c2[b] += 1
81-
for i in range(1, N):
82-
if c1[i] == 0 and c2[i] == n - 1:
85+
cnt1[a] += 1
86+
cnt2[b] += 1
87+
for i in range(1, n + 1):
88+
if cnt1[i] == 0 and cnt2[i] == n - 1:
8389
return i
8490
return -1
8591
```
@@ -91,15 +97,15 @@ class Solution:
9197
```java
9298
class Solution {
9399
public int findJudge(int n, int[][] trust) {
94-
int N = 1001;
95-
int[] c1 = new int[N];
96-
int[] c2 = new int[N];
97-
for (int[] e : trust) {
98-
++c1[e[0]];
99-
++c2[e[1]];
100+
int[] cnt1 = new int[n + 1];
101+
int[] cnt2 = new int[n + 1];
102+
for (var t : trust) {
103+
int a = t[0], b = t[1];
104+
++cnt1[a];
105+
++cnt2[b];
100106
}
101-
for (int i = 1; i < N; ++i) {
102-
if (c1[i] == 0 && c2[i] == n - 1) {
107+
for (int i = 1; i <= n; ++i) {
108+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
103109
return i;
104110
}
105111
}
@@ -108,42 +114,23 @@ class Solution {
108114
}
109115
```
110116

111-
### **TypeScript**
112-
113-
```ts
114-
function findJudge(n: number, trust: number[][]): number {
115-
let candidates = new Array(n).fill(0);
116-
for (let [a, b] of trust) {
117-
candidates[a - 1] = -1;
118-
if (candidates[b - 1] >= 0) {
119-
candidates[b - 1]++;
120-
}
121-
}
122-
123-
for (let i = 0; i < n; i++) {
124-
if (candidates[i] == n - 1) {
125-
return i + 1;
126-
}
127-
}
128-
return -1;
129-
}
130-
```
131-
132117
### **C++**
133118

134119
```cpp
135120
class Solution {
136121
public:
137122
int findJudge(int n, vector<vector<int>>& trust) {
138-
int N = 1001;
139-
vector<int> c1(N);
140-
vector<int> c2(N);
141-
for (auto& e : trust) {
142-
++c1[e[0]];
143-
++c2[e[1]];
123+
vector<int> cnt1(n + 1);
124+
vector<int> cnt2(n + 1);
125+
for (auto& t : trust) {
126+
int a = t[0], b = t[1];
127+
++cnt1[a];
128+
++cnt2[b];
144129
}
145-
for (int i = 1; i < N; ++i) {
146-
if (c1[i] == 0 && c2[i] == n - 1) return i;
130+
for (int i = 1; i <= n; ++i) {
131+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
132+
return i;
133+
}
147134
}
148135
return -1;
149136
}
@@ -154,22 +141,41 @@ public:
154141
155142
```go
156143
func findJudge(n int, trust [][]int) int {
157-
N := 1001
158-
c1 := make([]int, N)
159-
c2 := make([]int, N)
160-
for _, e := range trust {
161-
c1[e[0]]++
162-
c2[e[1]]++
144+
cnt1 := make([]int, n+1)
145+
cnt2 := make([]int, n+1)
146+
for _, t := range trust {
147+
a, b := t[0], t[1]
148+
cnt1[a]++
149+
cnt2[b]++
163150
}
164-
for i := 1; i < N; i++ {
165-
if c1[i] == 0 && c2[i] == n-1 {
151+
for i := 1; i <= n; i++ {
152+
if cnt1[i] == 0 && cnt2[i] == n-1 {
166153
return i
167154
}
168155
}
169156
return -1
170157
}
171158
```
172159

160+
### **TypeScript**
161+
162+
```ts
163+
function findJudge(n: number, trust: number[][]): number {
164+
const cnt1: number[] = new Array(n + 1).fill(0);
165+
const cnt2: number[] = new Array(n + 1).fill(0);
166+
for (const [a, b] of trust) {
167+
++cnt1[a];
168+
++cnt2[b];
169+
}
170+
for (let i = 1; i <= n; ++i) {
171+
if (cnt1[i] === 0 && cnt2[i] === n - 1) {
172+
return i;
173+
}
174+
}
175+
return -1;
176+
}
177+
```
178+
173179
### **...**
174180

175181
```

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

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@
6161
```python
6262
class Solution:
6363
def findJudge(self, n: int, trust: List[List[int]]) -> int:
64-
N = 1001
65-
c1, c2 = [0] * N, [0] * N
64+
cnt1 = [0] * (n + 1)
65+
cnt2 = [0] * (n + 1)
6666
for a, b in trust:
67-
c1[a] += 1
68-
c2[b] += 1
69-
for i in range(1, N):
70-
if c1[i] == 0 and c2[i] == n - 1:
67+
cnt1[a] += 1
68+
cnt2[b] += 1
69+
for i in range(1, n + 1):
70+
if cnt1[i] == 0 and cnt2[i] == n - 1:
7171
return i
7272
return -1
7373
```
@@ -77,15 +77,15 @@ class Solution:
7777
```java
7878
class Solution {
7979
public int findJudge(int n, int[][] trust) {
80-
int N = 1001;
81-
int[] c1 = new int[N];
82-
int[] c2 = new int[N];
83-
for (int[] e : trust) {
84-
++c1[e[0]];
85-
++c2[e[1]];
80+
int[] cnt1 = new int[n + 1];
81+
int[] cnt2 = new int[n + 1];
82+
for (var t : trust) {
83+
int a = t[0], b = t[1];
84+
++cnt1[a];
85+
++cnt2[b];
8686
}
87-
for (int i = 1; i < N; ++i) {
88-
if (c1[i] == 0 && c2[i] == n - 1) {
87+
for (int i = 1; i <= n; ++i) {
88+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
8989
return i;
9090
}
9191
}
@@ -94,42 +94,23 @@ class Solution {
9494
}
9595
```
9696

97-
### **TypeScript**
98-
99-
```ts
100-
function findJudge(n: number, trust: number[][]): number {
101-
let candidates = new Array(n).fill(0);
102-
for (let [a, b] of trust) {
103-
candidates[a - 1] = -1;
104-
if (candidates[b - 1] >= 0) {
105-
candidates[b - 1]++;
106-
}
107-
}
108-
109-
for (let i = 0; i < n; i++) {
110-
if (candidates[i] == n - 1) {
111-
return i + 1;
112-
}
113-
}
114-
return -1;
115-
}
116-
```
117-
11897
### **C++**
11998

12099
```cpp
121100
class Solution {
122101
public:
123102
int findJudge(int n, vector<vector<int>>& trust) {
124-
int N = 1001;
125-
vector<int> c1(N);
126-
vector<int> c2(N);
127-
for (auto& e : trust) {
128-
++c1[e[0]];
129-
++c2[e[1]];
103+
vector<int> cnt1(n + 1);
104+
vector<int> cnt2(n + 1);
105+
for (auto& t : trust) {
106+
int a = t[0], b = t[1];
107+
++cnt1[a];
108+
++cnt2[b];
130109
}
131-
for (int i = 1; i < N; ++i) {
132-
if (c1[i] == 0 && c2[i] == n - 1) return i;
110+
for (int i = 1; i <= n; ++i) {
111+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
112+
return i;
113+
}
133114
}
134115
return -1;
135116
}
@@ -140,22 +121,41 @@ public:
140121
141122
```go
142123
func findJudge(n int, trust [][]int) int {
143-
N := 1001
144-
c1 := make([]int, N)
145-
c2 := make([]int, N)
146-
for _, e := range trust {
147-
c1[e[0]]++
148-
c2[e[1]]++
124+
cnt1 := make([]int, n+1)
125+
cnt2 := make([]int, n+1)
126+
for _, t := range trust {
127+
a, b := t[0], t[1]
128+
cnt1[a]++
129+
cnt2[b]++
149130
}
150-
for i := 1; i < N; i++ {
151-
if c1[i] == 0 && c2[i] == n-1 {
131+
for i := 1; i <= n; i++ {
132+
if cnt1[i] == 0 && cnt2[i] == n-1 {
152133
return i
153134
}
154135
}
155136
return -1
156137
}
157138
```
158139

140+
### **TypeScript**
141+
142+
```ts
143+
function findJudge(n: number, trust: number[][]): number {
144+
const cnt1: number[] = new Array(n + 1).fill(0);
145+
const cnt2: number[] = new Array(n + 1).fill(0);
146+
for (const [a, b] of trust) {
147+
++cnt1[a];
148+
++cnt2[b];
149+
}
150+
for (let i = 1; i <= n; ++i) {
151+
if (cnt1[i] === 0 && cnt2[i] === n - 1) {
152+
return i;
153+
}
154+
}
155+
return -1;
156+
}
157+
```
158+
159159
### **...**
160160

161161
```

solution/0900-0999/0997.Find the Town Judge/Solution.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
class Solution {
22
public:
33
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-
++c1[e[0]];
9-
++c2[e[1]];
4+
vector<int> cnt1(n + 1);
5+
vector<int> cnt2(n + 1);
6+
for (auto& t : trust) {
7+
int a = t[0], b = t[1];
8+
++cnt1[a];
9+
++cnt2[b];
1010
}
11-
for (int i = 1; i < N; ++i) {
12-
if (c1[i] == 0 && c2[i] == n - 1) return i;
11+
for (int i = 1; i <= n; ++i) {
12+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
13+
return i;
14+
}
1315
}
1416
return -1;
1517
}

solution/0900-0999/0997.Find the Town Judge/Solution.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
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]]++
2+
cnt1 := make([]int, n+1)
3+
cnt2 := make([]int, n+1)
4+
for _, t := range trust {
5+
a, b := t[0], t[1]
6+
cnt1[a]++
7+
cnt2[b]++
88
}
9-
for i := 1; i < N; i++ {
10-
if c1[i] == 0 && c2[i] == n-1 {
9+
for i := 1; i <= n; i++ {
10+
if cnt1[i] == 0 && cnt2[i] == n-1 {
1111
return i
1212
}
1313
}

solution/0900-0999/0997.Find the Town Judge/Solution.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
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]];
3+
int[] cnt1 = new int[n + 1];
4+
int[] cnt2 = new int[n + 1];
5+
for (var t : trust) {
6+
int a = t[0], b = t[1];
7+
++cnt1[a];
8+
++cnt2[b];
99
}
10-
for (int i = 1; i < N; ++i) {
11-
if (c1[i] == 0 && c2[i] == n - 1) {
10+
for (int i = 1; i <= n; ++i) {
11+
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
1212
return i;
1313
}
1414
}

0 commit comments

Comments
 (0)