FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_03/id_118/leetcode_997_118.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_03
/
id_118
/
leetcode_997_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (36 loc) · 1.47 KB
Breadcrumbs
algorithm
/
Week_03
/
id_118
/
leetcode_997_118.java
Copy path
File metadata and controls
43 lines (36 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://leetcode-cn.com/problems/find-the-town-judge
// 997.找到小镇的法官
class
Solution
{
// 这道题可以解读为:
// 找出给定图中出度为0&&入度为N-1的点
// 这个点要么不存在,要么唯一
// 解题思路:
// N是顶点数,trust 是一个 M*2 的数组,其中 M 是边数。
// 若有一个点“出度0&&入度N-1”,则至少需要 N-1 条边。
public
int
findJudge
(
int
N
,
int
[][]
trust
) {
// 1. 取边数
int
M
=
trust
.
length
;
// 2. 边界处理:若边数<顶点数-1,则一定不存在要找的点
if
(
M
<
N
-
1
){
return
-
1
;
}
// 3. 遍历所有的边,分别统计每个顶点的入度和出度。初始时默认全是0。
// 注意:边的数字是从1开始的,不是0,所以入度出度表,要定义长度为N+1,而不是N
int
[]
in_degree
=
new
int
[
N
+
1
];
int
[]
out_degree
=
new
int
[
N
+
1
];
for
(
int
i
=
0
;
i
<
M
;
i
++){
// 边,from->to:from出度+1,to入度+1
int
from
=
trust
[
i
][
0
];
int
to
=
trust
[
i
][
1
];
out_degree
[
from
]+=
1
;
in_degree
[
to
]+=
1
;
}
// 4. 同时扫描入度出度表,找出度为0&&入度为N-1的点。
for
(
int
i
=
1
;
i
<=
N
;
i
++){
if
(
in_degree
[
i
]==
N
-
1
&&
out_degree
[
i
]==
0
){
return
i
;
}
}
return
-
1
;
}
}
Back
|
FazBrowse Home
|
New Git URL