8000 Add Java solution for day10: Find the town judge · qilingit/Leetcode@5f2acb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f2acb4

Browse files
committed
Add Java solution for day10: Find the town judge
1 parent 2b196b8 commit 5f2acb4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findJudge(int N, int[][] trust) {
3+
int[] array = new int[N];
4+
int in, out;
5+
6+
for(int i = 0; i < trust.length; i++) {
7+
in = trust[i][1];
8+
out = trust[i][0];
9+
array[in - 1] = array[in - 1] + 1;
10+
array[out - 1] = array[out - 1] - 1;
11+
}
12+
13+
// If a person only be trusted, and not trut anyone, then this person is judge.
14+
for(int j = 0; j < N; j++) {
15+
if(array[j] == N-1)
16+
return j+1;
17+
}
18+
19+
return -1;
20+
}
21+
}

0 commit comments

Comments
 (0)
0