Why my straightforward answer went wrong?


  • 0
    A

    I know it's a smart way to use bitwise after taking a look at other guys' answers.
    But can anyone tell me why my straightforward answer returned a Runtime Error " input: 1 failed" ?
    Thanks

    class Solution(object):
    
        def isPowerOfTwo(self, n):
      
            if n < 1:
                return False
            elif n = 1:
                return True
            else:
                i = 1
                N = 2    
                while N <= n:
                    N = N ** i
                    if N = n:
                        retun True
                    i += 1
                return False

  • 0
    W
    This post is deleted!

  • 1
    D

    Some mistakes, you should use == but not = (n = 1, N = n), return not retun,

    class Solution(object):
        def isPowerOfTwo(self, n):
            if n < 1:
                return False
            elif n == 1:
                return True
            else:
                i = 1
                N = 2    
                while N <= n:
                    N = N ** i
                    if N == n:
                        return True
                    i += 1
                return False

Log in to reply
 

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