Skip to content

Week03 和 Week04 作业 #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
leetCode_997_81
  • Loading branch information
okbengii committed May 15, 2019
commit 59bf088593569564f27ab262ab3f9ef12211f50f
39 changes: 39 additions & 0 deletions Week_03/id_81/LeetCode_997_81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
* 找到小镇的法官
*
* @link leetcode 997
* @author apple
*/
public class FindJudge {
// 目前短暂的思路: 把其想象成一个有向图,所以设置了两个map来保存,一个保 // 存其出度,一个保存其入度。如果某个的出度为0 ,入度为N-1,那就认为这个
// 值就是要找的法官
public static int findJudge(int N, int[][] trust) {
Map<Integer, Integer> output = new HashMap<>();
Map<Integer, Integer> input = new HashMap<>();

for (int n = 1; n <= N; ++n) {
output.put(n, 0);
input.put(n, 0);
}
for (int i = 0; i < trust.length; ++i) {
int[] arr = trust[i];
output.put(arr[1], output.get(arr[1]) + 1);
input.put(arr[0], input.get(arr[0]) - 1);
}
for (Entry<Integer, Integer> entry : output.entrySet()) {
int key = entry.getKey();
if (entry.getValue() == N - 1 && input.get(key) == 0)
return key;
}
return -1;
}

public static void main(String[] args) {
int[][] trust = new int[][] { { 1, 3 }, { 2, 3 }, { 3, 1 } };
System.out.println(findJudge(3, trust));
}
}