A题,不知道那里有错误
通过了81.25%,错误是“数组越界等非法访问情况”
求帮助
import java.util.*; /* * public class Point { * int x; * int y; * } */ public class Solution { /** * 返回牛牛能到达终点且不被淘汰的路径数 * @param n int整型 * @param Edge Point类一维数组 * @param f int整型一维数组 * @return int整型 */ List<Integer>[] list; int res; int[] f; boolean[] visit; public int solve(int n, Point[] Edge, int[] f) { // write code here list = new List[n+1]; visit = new boolean[n+1]; this.f = f; for (int i = 0; i < n; i++) { list[i] = new ArrayList<>(); } for (Point point : Edge) { list[point.x - 1].add(point.y - 1); list[point.y - 1].add(point.x - 1); } res = 0; visit[0] = true; dfs(0, 0); return res; } public void dfs(int x, int g) { if (f[x] == 1) { g++; } List<Integer> list1 = list[x]; int y = 0; for (Integer integer : list1) { if (!visit[integer]) { visit[integer] = true; y++; dfs(integer, g); } } if (y == 0 && g <= 2) { res++; } } }