Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. class Solution {
  2. public Solution() {}
  3.  
  4. public int solution(int[] A) {
  5.  
  6. int next = A[0] + 0;
  7. int N = A.length;
  8. Map<Integer, Integer> count = new HashMap<Integer, Integer>();
  9. for (int i=0; i<N; i++) {
  10. count.put(i, 0);
  11. }
  12.  
  13. count.put(0, 1); //already pass position 0 once
  14. int times = 0;
  15. while (next < N && next >= 0) { /* next index can be negative!!!!! Error!!!! */
  16. times ++;
  17. next = A[next] + next;
  18. System.out.println("Now the next index is: " + next);
  19.  
  20. if (next >= N || next < 0) { /* next index can be negative!!!!! Error!!!! */
  21. System.out.println("Now jump out. ");
  22. return times+1;
  23. }
  24. count.put(next, count.get(next) + 1);
  25. if (count.get(next) > 1) {
  26. System.out.println("Repeated index " + next + ", will not jump out");
  27. return -1;
  28. } else {
  29. continue;
  30. }
  31.  
  32. }
  33. return times;
  34. }
  35. }
Add Comment
Please, Sign In to add comment