Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public Solution() {}
- public int solution(int[] A) {
- int next = A[0] + 0;
- int N = A.length;
- Map<Integer, Integer> count = new HashMap<Integer, Integer>();
- for (int i=0; i<N; i++) {
- count.put(i, 0);
- }
- count.put(0, 1); //already pass position 0 once
- int times = 0;
- while (next < N && next >= 0) { /* next index can be negative!!!!! Error!!!! */
- times ++;
- next = A[next] + next;
- System.out.println("Now the next index is: " + next);
- if (next >= N || next < 0) { /* next index can be negative!!!!! Error!!!! */
- System.out.println("Now jump out. ");
- return times+1;
- }
- count.put(next, count.get(next) + 1);
- if (count.get(next) > 1) {
- System.out.println("Repeated index " + next + ", will not jump out");
- return -1;
- } else {
- continue;
- }
- }
- return times;
- }
- }
Add Comment
Please, Sign In to add comment