clean & simple python (88ms)

  • 0
    class Solution(object):
        def arrayNesting(self, nums):
            :type nums: List[int]
            :rtype: int
            There can be multiple cycles in the collection (array).
            And you will never visit any item of the cycle twice.
            If you do, it means that two positions in the collection hold
            the same index, which is impossible as repetitions are forbidden
            in the question. The only way you can re-visit an element of the cycle
            is when you are just starting out, which means you haven't really started
            adding any items into the cycle.
            ans, step, visited = 0, 0, [False] * len(nums)
            for i in range(len(nums)):
                while not visited[i]:
                    visited[i], i, step = True, nums[i], step+1
                ans, step = max(ans,step), 0
            return ans

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.