|
3 | 3 | importjava.util.HashSet;
|
4 | 4 | importjava.util.Set;
|
5 | 5 |
|
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 |
| - */ |
49 | 6 | 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; |
62 | 22 | }
|
63 |
| - } |
64 |
| -returnjudge; |
65 | 23 | }
|
66 |
| - } |
67 | 24 | }
|