跳转至

50.powx-n

Statement

Metadata
  • Link: Pow(x, n)
  • Difficulty: Medium
  • Tag: 递归 数学

实现 pow(x, n) ,即计算 xn 次幂函数(即,xn )。

 

示例 1:

输入:x = 2.00000, n = 10
输出:1024.00000

示例 2:

输入:x = 2.10000, n = 3
输出:9.26100

示例 3:

输入:x = 2.00000, n = -2
输出:0.25000
解释:2-2 = ½2 = ¼ = 0.25

 

提示:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • -104 <= xn <= 104

Metadata
  • Link: Pow(x, n)
  • Difficulty: Medium
  • Tag: Recursion Math

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

 

Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000

Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100

Example 3:

Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = ½2 = ¼ = 0.25

 

Constraints:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • -104 <= xn <= 104

Solution

class Solution:
    def myPow(self, x: float, n: int) -> float:
        return x ** n

最后更新: October 11, 2023
回到页面顶部