Pow(x, n)

🎯 问题描述(来源于LeetCode)

1
实现pow(_x_, _n_) ,即计算x的整n次幂函数(即,x^n )。

💻 代码实现

1
2
3
4
5
6
7
8
9
10
class Solution:
    def myPow(self, x: float, n: int) -> float:
         ans=1.0
         i=0
         if n<0:
            x=1/x
            n=-n
         for i in range(n):
            ans*=x
         return ans

使用快速幂降低其时间复杂度

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def myPow(self, x: float, n: int) -> float:
ans=1.0
i=0
if n<0:
x=1/x
n=-n
while n:
if(n&1==1):
ans*=x
x*=x
n>>=1
return ans

📊 性能分析

提交结果

  • 运行时间:0ms击败100.00 %
  • 内存消耗:17.40MB击败 94.00%

复杂度验证

  • 时间复杂度:O(log(n))O(log(n))
  • 空间复杂度:O(1)O(1)