Weird python problem between 'is' and '=='.


  • 0
    D

    right code:

    class Solution(object):
        def countBits(self, num):
            lst = [0] * (num + 1)
            milestone = 0
            nextMilestone = 2
            for i in range(1, num + 1):
                if i == nextMilestone:
                    milestone = nextMilestone
                    nextMilestone = nextMilestone * 2
    
                lst[i] = lst[i - milestone] + 1
            return lst
    

    the code above is my right solution , and I think it's perfect. But the problem is when I replace the '==' to 'is' , I got a "Output Limit Exceeded"! please tell me why T_T

    wrong code:

    class Solution(object):
        def countBits(self, num):
            lst = [0] * (num + 1)
            milestone = 0
            nextMilestone = 2
            for i in range(1, num + 1):
                if i is nextMilestone:
                    milestone = nextMilestone
                    nextMilestone = nextMilestone * 2
    
                lst[i] = lst[i - milestone] + 1
            return lst

Log in to reply
 

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