public boolean bfs(int[] nums, int start) { if (start < 0 || start > nums.length || nums[start] == -1) return false; Queue q = new LinkedList(); q.add(start);
Set set = new HashSet(); set.add(start);
while (q.size() > 0) { int poll = (int) q.poll(); if (nums[poll] == 0) return true; for (int i : new int[]{-1, 1}) { int next = poll + i * nums[poll]; if (!set.contains(next) && next >= 0 && next < nums.length) { q.add(next); set.add(next); } } } return false; }