Skip to content

Commit 391b30e

Browse files
committed
Add solution and test-cases for problem 997
1 parent ca207cf commit 391b30e

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

leetcode/901-1000/0997.Find-the-Town-Judge/README.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
# [997.Find the Town Judge][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
In a town, there are `n` people labeled from `1` to `n`. There is a rumor that one of these people is secretly the town judge.
75

8-
**Example 1:**
6+
If the town judge exists, then:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
1. The town judge trusts nobody.
9+
2. Everybody (except for the town judge) trusts the town judge.
10+
3. There is exactly one person that satisfies properties **1** and **2**.
11+
12+
You are given an array `trust` where `trust[i] = [ai, bi]` representing that the person labeled a<sub>i</sub> trusts the person labeled b<sub>i</sub>.
13+
14+
Return the label of the town judge if the town judge exists and can be identified, or return `-1` otherwise.
1415

15-
## 题意
16-
> ...
1716

18-
## 题解
17+
**Example 1:**
1918

20-
### 思路1
21-
> ...
22-
Find the Town Judge
23-
```go
19+
```
20+
Input: n = 2, trust = [[1,2]]
21+
Output: 2
2422
```
2523

24+
**Example 2:**
25+
```
26+
Input: n = 3, trust = [[1,3],[2,3]]
27+
Output: 3
28+
```
2629

30+
**Example 3:**
31+
```
32+
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
33+
Output: -1
34+
```
2735
## 结语
2836

2937
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, trust [][]int) int {
4+
ab, ba := make([]int, n+1), make([]int, n+1)
5+
for _, t := range trust {
6+
a, b := t[0], t[1]
7+
ab[a]++
8+
ba[b]++
9+
}
10+
for p := 1; p <= n; p++ {
11+
if ab[p] == 0 && ba[p] == n-1 {
12+
return p
13+
}
14+
}
15+
return -1
516
}

leetcode/901-1000/0997.Find-the-Town-Judge/Solution_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
inputs [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, [][]int{{1, 2}}, 2},
18+
{"TestCase2", 3, [][]int{{1, 3}, {2, 3}}, 3},
19+
{"TestCase3", 3, [][]int{{1, 3}, {2, 3}, {3, 1}}, -1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.inputs)
2526
if !reflect.DeepEqual(got, c.expect) {
2627
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2728
c.expect, got, c.inputs)

0 commit comments

Comments
 (0)