Movatterモバイル変換


[0]ホーム

URL:


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

Commit2b4fb7b

Browse files
refactor 997
1 parent4bd4971 commit2b4fb7b

File tree

1 file changed

+15
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-58
lines changed

‎src/main/java/com/fishercoder/solutions/_997.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,22 @@
33
importjava.util.HashSet;
44
importjava.util.Set;
55

6-
/**
7-
* 997. Find the Town Judge
8-
*
9-
* In a town, there are N people labelled from 1 to N.
10-
* There is a rumor that one of these people is secretly the town judge.
11-
*
12-
* If the town judge exists, then:
13-
*
14-
* The town judge trusts nobody.
15-
* Everybody (except for the town judge) trusts the town judge.
16-
* There is exactly one person that satisfies properties 1 and 2.
17-
* You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
18-
*
19-
* If the town judge exists and can be identified, return the label of the town judge.
20-
* Otherwise, return -1.
21-
*
22-
* Example 1:
23-
* Input: N = 2, trust = [[1,2]]
24-
* Output: 2
25-
*
26-
* Example 2:
27-
* Input: N = 3, trust = [[1,3],[2,3]]
28-
* Output: 3
29-
*
30-
* Example 3:
31-
* Input: N = 3, trust = [[1,3],[2,3],[3,1]]
32-
* Output: -1
33-
*
34-
* Example 4:
35-
* Input: N = 3, trust = [[1,2],[2,3]]
36-
* Output: -1
37-
*
38-
* Example 5:
39-
* Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
40-
* Output: 3
41-
*
42-
* Note:
43-
* 1 <= N <= 1000
44-
* trust.length <= 10000
45-
* trust[i] are all different
46-
* trust[i][0] != trust[i][1]
47-
* 1 <= trust[i][0], trust[i][1] <= N
48-
*/
496
publicclass_997 {
50-
publicstaticclassSolution1 {
51-
publicintfindJudge(intN,int[][]trust) {
52-
int[]trustPoints =newint[N];
53-
Set<Integer>trustOthers =newHashSet<>();
54-
for (int[]eachTrust :trust) {
55-
trustPoints[eachTrust[1] -1]++;
56-
trustOthers.add(eachTrust[0]);
57-
}
58-
intjudge = -1;
59-
for (inti =0;i <trustPoints.length;i++) {
60-
if (trustPoints[i] ==N -1 && !trustOthers.contains(i +1)) {
61-
judge =i +1;
7+
publicstaticclassSolution1 {
8+
publicintfindJudge(intN,int[][]trust) {
9+
int[]trustPoints =newint[N];
10+
Set<Integer>trustOthers =newHashSet<>();
11+
for (int[]eachTrust :trust) {
12+
trustPoints[eachTrust[1] -1]++;
13+
trustOthers.add(eachTrust[0]);
14+
}
15+
intjudge = -1;
16+
for (inti =0;i <trustPoints.length;i++) {
17+
if (trustPoints[i] ==N -1 && !trustOthers.contains(i +1)) {
18+
judge =i +1;
19+
}
20+
}
21+
returnjudge;
6222
}
63-
}
64-
returnjudge;
6523
}
66-
}
6724
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp