LeetCode 565 数组嵌套

  1. 将遍历过的数置为-1
    1. var arrayNesting = function (nums) {
    2. let Max = 0;
    3. for (let i = 0; i < nums.length; i++) {
    4. let max = 0;
    5. for (let j = i; nums[j] !== -1;) {
    6. //nums[j] 等于-1说明遍历过了,退出循环
    7. max++;
    8. let t = nums[j];
    9. nums[j] = -1 //遍历过就置为-1
    10. j = t;
    11. }
    12. Max = Math.max(Max, max);
    13. }
    14. return Max;
    15. };