classSolution: deffindJudge(self, n: int, trust: List[List[int]]) -> int: inDegrees = Counter(y for _, y in trust) outDegrees = Counter(x for x, _ in trust) returnnext((i for i inrange(1, n + 1) if inDegrees[i] == n - 1and outDegrees[i] == 0), -1)
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintfindJudge(int n, int[][] trust) { int[] inDegrees = newint[n + 1]; int[] outDegrees = newint[n + 1]; for (int[] edge : trust) { intx= edge[0], y = edge[1]; ++inDegrees[y]; ++outDegrees[x]; } for (inti=1; i <= n; ++i) { if (inDegrees[i] == n - 1 && outDegrees[i] == 0) { return i; } } return -1; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicintFindJudge(int n, int[][] trust) { int[] inDegrees = newint[n + 1]; int[] outDegrees = newint[n + 1]; foreach (int[] edge in trust) { int x = edge[0], y = edge[1]; ++inDegrees[y]; ++outDegrees[x]; } for (int i = 1; i <= n; ++i) { if (inDegrees[i] == n - 1 && outDegrees[i] == 0) { return i; } } return-1; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intfindJudge(int n, vector<vector<int>>& trust){ vector<int> inDegrees(n + 1); vector<int> outDegrees(n + 1); for (auto& edge : trust) { int x = edge[0], y = edge[1]; ++inDegrees[y]; ++outDegrees[x]; } for (int i = 1; i <= n; ++i) { if (inDegrees[i] == n - 1 && outDegrees[i] == 0) { return i; } } return-1; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intfindJudge(int n, int** trust, int trustSize, int* trustColSize){ int* inDegrees = (int *)malloc(sizeof(int)*(n+1)); int* outDegrees = (int *)malloc(sizeof(int)*(n+1)); memset(inDegrees, 0, sizeof(int)*(n+1)); memset(outDegrees, 0, sizeof(int)*(n+1)); for (int i = 0; i < trustSize; ++i) { int x = trust[i][0], y = trust[i][1]; ++inDegrees[y]; ++outDegrees[x]; } for (int i = 1; i <= n; ++i) { if (inDegrees[i] == n - 1 && outDegrees[i] == 0) { return i; } } return-1; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var findJudge = function(n, trust) { const inDegrees = newArray(n + 1).fill(0); const outDegrees = newArray(n + 1).fill(0); for (const edge of trust) { const x = edge[0], y = edge[1]; ++inDegrees[y]; ++outDegrees[x]; } for (let i = 1; i <= n; ++i) { if (inDegrees[i] === n - 1 && outDegrees[i] === 0) { return i; } } return -1; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcfindJudge(n int, trust [][]int)int { inDegrees := make([]int, n+1) outDegrees := make([]int, n+1) for _, t := range trust { inDegrees[t[1]]++ outDegrees[t[0]]++ } for i := 1; i <= n; i++ { if inDegrees[i] == n-1 && outDegrees[i] == 0 { return i } } return-1 }