2 methods for python: iteration and recursion


  • 0
    K
    def ipow(x, n):
        if n == 0:
             return 1
        else:
            original = num
            for i in range(n-1):
                num *= original
                n -= 1
            return num
    
    def rpow(x, n):
        if n == 0:
            return 1
        else:
            return num*rpow(num, n-1)
    

    """ runtime beats 99% submissions, I did not consider negative n but it is easy, just return 1/num is enough"""


Log in to reply
 

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