赞
踩
实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,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 = 1/22 = 1/4 = 0.25
提示:
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104
注意:本题与主站 50 题相同:https://leetcode-cn.com/problems/powx-n/
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import * class Solution: def __init__(self): pass def myPow(self, x: float, n: int) -> float: if x == 0: return 0 res = 1 if n < 0: x, n = 1 / x, -n while n: if n & 1: res *= x x *= x n >>= 1 return res def myPow_02(self, x: float, n: int) -> float: if n==0:return 1 elif n<0:return 1/self.myPow_02(x,-n) elif n&1:return x*self.myPow_02(x,n-1) else: return self.myPow_02(x*x,n//2) def myPow_ac(self, x: float, n: int) -> float: return x**n def test(data_test): s = Solution() return s.getResult(*data_test) def test_obj(data_test): result = [None] obj = Solution(*data_test[1][0]) for fun, data in zip(data_test[0][1::], data_test[1][1::]): if data: res = obj.__getattribute__(fun)(*data) else: res = obj.__getattribute__(fun)() result.append(res) return result if __name__ == '__main__': datas = [ [], ] for data_test in datas: t0 = time.time() print('-' * 50) print('input:', data_test) print('output:', test(data_test)) print(f'use time:{time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。