美文网首页
leetcode 457. 环形数组是否存在循环

leetcode 457. 环形数组是否存在循环

作者: 七齐起器 | 来源:发表于2021-04-30 11:21 被阅读0次
    class Solution(object):
        def circularArrayLoop(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            for i in range(0,len(nums)):
                k=[]
                xiabiao=i
                k.append(xiabiao)
                while len(k)<=len(nums):
                    xiabiao=(xiabiao+nums[xiabiao])%len(nums)
                    if len(k)>1 and xiabiao in k : return True 
                    # print xiabiao,nums[xiabiao],nums[k[0]]
                    if (nums[xiabiao]>0 and nums[k[0]]>0) or (nums[xiabiao]<0 and nums[k[0]]<0):
                        if nums[xiabiao]%len(nums)==0 :
                            break
                        else:
                            k.append(xiabiao)
                    else:
                        break
                # print k
            return False
    
    

    相关文章

      网友评论

          本文标题:leetcode 457. 环形数组是否存在循环

          本文链接:https://www.haomeiwen.com/subject/csdnrltx.html