You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int findJudge(int N, int[][] trust) {
int[][] people = new int[N][2];
for(int[] person : trust){
int out = person[0];
int in = person[1];
people[out - 1][0] ++;
people[in - 1][1] ++;
}
for(int i = 0; i < N; i ++){
if(people[i][0] == 0 && people[i][1] == N - 1)
return i + 1;
}
return -1;
}
}
class Solution {
public int findJudge(int N, int[][] trust) {
int[] people = new int[N];
for(int[] aaa : trust){
people[aaa[1]-1] ++;
people[aaa[0]-1] --;
}
for(int i = 0; i < N; i ++){
if(people[i] == N - 1)
return i+1;
}
return -1;
}
}
和方法二相比很类似,但是更简便,用一维数组即可记录入度为N-1的值
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
这次的week03 很多超出了我之前学的算法范围,dfs和bfs和图都是我之前没学过的,都是看着争哥的专栏边学边写,说实话,图我还能看懂,自己写了出来,但是dfs和bfs我都是最后写不出来借鉴的别人的方法,自己又写了一遍。下面我就说说我写的图的那题
Find the Town Judge
的解决方法。上面的方法是我自己想的,比较暴力,循环3次,先把有入度的都记下来,key是label,value是入度的次数,然后第2次遍历是把有出度的key给删了,第3次遍历是看哪个key的value值等于N-1,那这个key就是法官,比较耗内存和cpu。
方法二:
是用一个二维数组将入度和出度都记下来,最后判断出度为0且入度为N-1的label为法官,这个就比我自己想的简单很多,资源使用也少。
方法三:
和方法二相比很类似,但是更简便,用一维数组即可记录入度为N-1的值
The text was updated successfully, but these errors were encountered: