Please help me with this code


  • 0
    K

    This code will give the wrong answer when I tested it. But I cannot find which part is wrong. I am not very clear about how to use yield, maybe this is the reason. Someone, please help me. Any help will be appreciated.

    def canCross(self, stones):
        def cross(stones, jump):
            
            if len(stones) == 2 and (jump - 1) <= stones[1] - stones[0] <= (jump + 1):
                    yield True
                    
            for i in range(1, len(stones)):
                if jump + 1 < stones[i] - stones[0]:
                    yield False
                elif jump - 1 <= stones[i] - stones[0] <= jump + 1:
                    yield cross(stones[1:], stones[i] - stones[0])
                elif i == len(stones) - 1 and jump - 1 > stones[i] - stones[0]:
                    yield False
        if len(stones) <= 2:
            return True
        jump = 1
        return any(cross(stones, jump))
    

Log in to reply
 

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