美文网首页
Leetcode50-Pow(x, n)(Python3)

Leetcode50-Pow(x, n)(Python3)

作者: LdpcII | 来源:发表于2017-09-27 17:18 被阅读0次

50. Pow(x, n)

Implement pow(x, n).

My Solution

class Solution(object):
    myPow = pow

Reference (转)

1
class Solution:
    def myPow(self, x, n):
        return x ** n
2
class Solution:
    def myPow(self, x, n):
        if not n:
            return 1
        if n < 0:
            return 1 / self.myPow(x, -n)
        if n % 2:
            return x * self.myPow(x, n-1)
        return self.myPow(x*x, n/2)
3
class Solution:
    def myPow(self, x, n):
        if n < 0:
            x = 1 / x
            n = -n
        pow = 1
        while n:
            if n & 1:
                pow *= x
            x *= x
            n >>= 1
        return pow

相关文章

  • Leetcode50-Pow(x, n)(Python3)

    50. Pow(x, n) Implement pow(x, n). My Solution Reference ...

  • Leetcode50-Pow(x,n)

    题目: Implement pow(x, n), which calculates x raised to the...

  • 50. Pow(x, n)

    Implement pow(x, n).实现x的n次方。利用x^n = x^(n/2) * x^(n/2) *x^...

  • Pow(x,n)

    x的N次方可以看做:x^n = x(n/2)*x(n/2)*x(n%2)。利用递归求解,当n==1的时候,xn=x...

  • a=1<<2含义

    x<< n=x2^n,操作速度上x<< n要比x2^n快。

  • 阶乘

    问题 输入一个正整数 n,输出 n! 的值。其中 n! = 1 x 2 x 3 x … x n,n! < Inte...

  • 20180919-正则表达式

    X?0次或1次X*0次或多次X+1次或多次X{n}刚好n次X{n,}至少n次X{n,m}至少n次至多m次\d数字[...

  • 2019-05-15 尾递归跟python无关...

    我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出: fact(n...

  • 分治法的常见问题

    计算x的n次幂 朴素算法:xxx...... 分治算法: n为偶数:x的n/2次幂*x的n/2次幂 n为奇数:x的...

  • LeetCode题解:数的N次方

    题目描述 实现Pow(x,n),即计算x的n次幂函数(即,x^n)。 示例 示例1输入:x = 2.00000, ...

网友评论

      本文标题:Leetcode50-Pow(x, n)(Python3)

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