public boolean dfs(int[] nums, int start, Set set) { if (start < 0 || start > nums.length || nums[start] == -1) return false; if (nums[start] == 0) return true; for (int i : new int[]{-1, 1}) { int next = start + i * nums[start]; if (!set.contains(next) && next >= 0 && next < nums.length) { set.add(next); if (dfs(nums, next, set)) return true; set.remove(next); } } return false; }