Python sulution


  • 0
    B

    class Solution(object):
    def circularArrayLoop(self, nums):
    """
    :type nums: List[int]
    :rtype: bool
    """
    if not nums: return False

        path=[]; uniq=set()
        curr=0
        forward=backward=0
        while curr not in uniq:
            path.append(curr)
            uniq.add(curr)
            if curr+nums[curr]>=len(nums):forward+=1
            elif curr+nums[curr]<0: backward+=1    
            curr=(curr+nums[curr])%len(nums)
        return curr==path[0] and bool((forward and not backward) or (backward and not forward))

Log in to reply
 

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