题目描述:
LCP 07. 传递信息 - 力扣(LeetCode) (leetcode-cn.com)
自测用例:
5
[[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]]
4
3
[[0,2],[2,1]]
3
5
[[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]]
3
3
[[0,2],[2,1]]
2
Java代码:
class Solution {
int ans=0;
public void go(List<Integer>[] a,int x,int k){
if(k--!=0)for(int y:a[x])go(a,y,k);
else if(x==a.length-1)ans++;
}
public int numWays(int n, int[][] r, int k) {
List<Integer>[] a=new List[n];
for(int i=0;i<a.length;i++)a[i]=new LinkedList<>();
for(int[] xy:r)a[xy[0]].add(xy[1]);
go(a,0,k);
return ans;
}
}
?
|