Twitter OA prepare: Visit element of the array
分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数
1 public int visiting(int[] A, int N) { 2 if (A==null || A.length==0) return 0; 3 int cur = 0; 4 int count = 0; 5 boolean[] visited = new boolean[N]; 6 while (cur>=0 && cur<A.length && !visited[cur]) { 7 visited[cur] = true; 8 cur = cur + A[cur]; 9 count++; 10 } 11 return N-count; 12 }Twitter OA prepare: Visit element of the array