Find the Town Judge LeetCode solution in Java

1
class Solution {
2
public int findJudge(int n, int[][] trust) {
3
int count = 0;
4
int x[] = new int[n + 1];
5
int y[] = new int[n + 1];
6
Arrays.fill(x, 0);
7
Arrays.fill(y, 0);
8
for (int i = 0; i < trust.length; i++) {
9
x[trust[i][0]] = 1;
10
}
11
for (int i = 1; i <= n; i++) {
12
if (x[i] != 0) count++;
13
if (x[i] == 0) y[i] = 1;
14
}
15
if (count == n) return -1;
16
for (int i = 1; i <= n; i++) System.out.println(y[i]);
17
int jud = 0;
18
for (int i = 1; i <= n; i++) {
19
if (y[i] == 1) jud = i;
20
}
21
int c = 0;
22
for (int i = 0; i < trust.length; i++) {
23
if (trust[i][1] == jud) c++;
24
}
25
if (c == n - 1) return jud;
26
return -1;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0