Skip to content

Commit 1499e78

Browse files
authored
Merge pull request halfrost#216 from gostool/leetcode0997
Leetcode0997
2 parents a5d665c + 4b6a883 commit 1499e78

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
func findJudge(n int, trust [][]int) int {
4+
if n == 1 && len(trust) == 0 {
5+
return 1
6+
}
7+
judges := make(map[int]int)
8+
for _, v := range trust {
9+
judges[v[1]] += 1
10+
}
11+
for _, v := range trust {
12+
if _, ok := judges[v[0]]; ok {
13+
delete(judges, v[0])
14+
}
15+
}
16+
for k, v := range judges {
17+
if v == n-1 {
18+
return k
19+
}
20+
}
21+
return -1
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question997 struct {
9+
para997
10+
ans997
11+
}
12+
13+
// para 是参数
14+
type para997 struct {
15+
n int
16+
trust [][]int
17+
}
18+
19+
// ans 是答案
20+
type ans997 struct {
21+
ans int
22+
}
23+
24+
func Test_Problem997(t *testing.T) {
25+
26+
qs := []question997{
27+
28+
{
29+
para997{2, [][]int{{1, 2}}},
30+
ans997{2},
31+
},
32+
33+
{
34+
para997{3, [][]int{{1, 3}, {2, 3}}},
35+
ans997{3},
36+
},
37+
38+
{
39+
para997{3, [][]int{{1, 3}, {2, 3}, {3, 1}}},
40+
ans997{-1},
41+
},
42+
}
43+
44+
fmt.Printf("------------------------Leetcode Problem 997------------------------\n")
45+
46+
for _, q := range qs {
47+
_, p := q.ans997, q.para997
48+
fmt.Printf("【input】:%v 【output】:%v\n", p, findJudge(p.n, p.trust))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [997. Find the Town Judge](https://leetcode-cn.com/problems/find-the-town-judge/)
2+
3+
## 题目
4+
5+
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.
6+
7+
If the town judge exists, then:
8+
9+
- The town judge trusts nobody.
10+
- Everybody (except for the town judge) trusts the town judge.
11+
- There is exactly one person that satisfies properties 1 and 2.
12+
13+
You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi.
14+
15+
Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
16+
17+
**Example 1**:
18+
19+
Input: n = 2, trust = [[1,2]]
20+
Output: 2
21+
22+
**Example 2**:
23+
24+
Input: n = 3, trust = [[1,3],[2,3]]
25+
Output: 3
26+
27+
**Example 3**:
28+
29+
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
30+
Output: -1
31+
32+
**Constraints:**
33+
34+
- 1 <= n <= 1000
35+
- 0 <= trust.length <= 10000
36+
- trust[i].length == 2
37+
- All the pairs of trust are unique.
38+
- ai != bi
39+
- 1 <= ai, bi <= n
40+
41+
## 题目大意
42+
43+
小镇里有 n 个人,按从 1 到 n 的顺序编号。传言称,这些人中有一个暗地里是小镇法官。
44+
45+
如果小镇法官真的存在,那么:
46+
47+
- 小镇法官不会信任任何人。
48+
- 每个人(除了小镇法官)都信任这位小镇法官。
49+
- 只有一个人同时满足属性 1 和属性 2 。
50+
51+
给你一个数组 trust ,其中 trust[i] = [ai, bi] 表示编号为 ai 的人信任编号为 bi 的人。
52+
53+
如果小镇法官存在并且可以确定他的身份,请返回该法官的编号;否则,返回 -1 。
54+
55+
## 解题思路
56+
57+
入度和出度统计
58+
59+
- 被人信任定义为入度, 信任别人定义为出度
60+
- 如果1-n之间有数字x的入度为n - 1,出度为0,则返回x
61+
62+
## 代码
63+
64+
```go
65+
package leetcode
66+
67+
func findJudge(n int, trust [][]int) int {
68+
if n == 1 && len(trust) == 0 {
69+
return 1
70+
}
71+
judges := make(map[int]int)
72+
for _, v := range trust {
73+
judges[v[1]] += 1
74+
}
75+
for _, v := range trust {
76+
if _, ok := judges[v[0]]; ok {
77+
delete(judges, v[0])
78+
}
79+
}
80+
for k, v := range judges {
81+
if v == n-1 {
82+
return k
83+
}
84+
}
85+
return -1
86+
}
87+
```

0 commit comments

Comments
 (0)