Pow(x, n)
实现 pow(x, n)(https://www.cplusplus.com/reference/valarray/pow/) ,即计算 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
解法参考链接:https://blog.csdn.net/weixin_43813003/article/details/102020554
算法思路:
Pow(x, n)求幕即计算 x 的 n 次幂函数,以计算2的11次方(2028)为例,常规算法是计算10次2与原数相乘。
为了简化计算,可以先计算出2×2=4的值,这样2的11次方可以写成4X4×4X4×4×2(此处多余一个4×2)的形式,再计算4X4=16的值,则2的11次方可以写成16×16×4×2的值,这样计算2X2,4×4,16×16,4X2的值,只计算了5次即得出结果。
由于计算机执行的是二进制,所以可以通过位运算进行计算。对上述幂算法进行优化,例如判断n是否偶数,可以使用“按位与”运算符“&”与1进行与计算,即判断n的值是否为0即可。而n=n/2(即阶乘每次降低1半)可以使用“右移”运算符“>>”,即n>>=1来操作。
例如:求解2^11
求解变量值记录如下:
--*--Source path:... E:/BLOG/python_day/python_day_code/D.py
--*--Starting var:.. self = <__main__.Solution object at 0x000002025F6867B8>
--*--Starting var:.. x = 2.0
--*--Starting var:.. n = 11
--*--14:02:56.664210 call 5 def myPow(self, x, n):
--*--14:02:56.664210 line 6 if n == 0:
--*--14:02:56.664210 line 8 res ,curr = 1, abs(n)
--*--New var:....... res = 1
--*--New var:....... curr = 11
--*--14:02:56.664210 line 9 while curr > 0:
--*--14:02:56.664210 line 10 if curr & 1 == 1:
--*--14:02:56.664210 line 11 res *= x
--*--Modified var:.. res = 2.0
--*--14:02:56.664210 line 12 curr >>= 1
--*--Modified var:.. curr = 5
--*--14:02:56.664210 line 13 x *= x
--*--Modified var:.. x = 4.0
--*--14:02:56.664210 line 9 while curr > 0:
--*--14:02:56.664210 line 10 if curr & 1 == 1:
--*--14:02:56.664210 line 11 res *= x
--*--Modified var:.. res = 8.0
--*--14:02:56.664210 line 12 curr >>= 1
--*--Modified var:.. curr = 2
--*--14:02:56.679833 line 13 x *= x
--*--Modified var:.. x = 16.0
--*--14:02:56.679833 line 9 while curr > 0:
--*--14:02:56.679833 line 10 if curr & 1 == 1:
--*--14:02:56.679833 line 12 curr >>= 1
--*--Modified var:.. curr = 1
--*--14:02:56.679833 line 13 x *= x
--*--Modified var:.. x = 256.0
--*--14:02:56.679833 line 9 while curr > 0:
--*--14:02:56.679833 line 10 if curr & 1 == 1:
--*--14:02:56.679833 line 11 res *= x
--*--Modified var:.. res = 2048.0
--*--14:02:56.679833 line 12 curr >>= 1
--*--Modified var:.. curr = 0
--*--14:02:56.679833 line 13 x *= x
--*--Modified var:.. x = 65536.0
--*--14:02:56.679833 line 9 while curr > 0:
--*--14:02:56.679833 line 14 if n < 0:
--*--14:02:56.679833 line 16 return res
--*--14:02:56.679833 return 16 return res
--*--Return value:.. 2048.0
--*--Elapsed time: 00:00:00.015623
最终得出x = 2048
示例代码:
import pysnooper #pip install pysnooper后重新加载IDE后使用
@pysnooper.snoop("./log/debug.log", prefix="--*--") #需提前建立目录及文件
class Solution:
def myPow(self, x, n):
if n == 0:
return 1
res ,curr = 1, abs(n)
while curr > 0:
if curr & 1 == 1:
res *= x
curr >>= 1
x *= x
if n < 0:
return 1 / res
return res
# %%
s = Solution()
print(s.myPow(x = 2.00000, n = 11))
PS:日志输出神器PySnooper
项目地址:GitHub - cool-RR/PySnooper: Never use print for debugging again
便捷安装:pip install pysnooper
PySnooper-不再使用打印进行调试
官方介绍及DEMO
PySnooper - Never use print for debugging again
PySnooper is a poor man's debugger. If you've used Bash, it's like set -x
for Python, except it's fancier.
Your story: You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now.
You want to know which lines are running and which aren't, and what the values of the local variables are.
Most people would use print
lines, in strategic locations, some of them showing the values of variables.
PySnooper lets you do the same, except instead of carefully crafting the right print
lines, you just add one decorator line to the function you're interested in. You'll get a play-by-play log of your function, including which lines ran and when, and exactly when local variables were changed.
What makes PySnooper stand out from all other code intelligence tools? You can use it in your shitty, sprawling enterprise codebase without having to do any setup. Just slap the decorator on, as shown below, and redirect the output to a dedicated log file by specifying its path as the first argument.
Example
We're writing a function that converts a number to binary, by returning a list of bits. Let's snoop on it by adding the @pysnooper.snoop()
decorator:
import pysnooper
@pysnooper.snoop()
def number_to_bits(number):
if number:
bits = []
while number:
number, remainder = divmod(number, 2)
bits.insert(0, remainder)
return bits
else:
return [0]
number_to_bits(6)
The output to stderr is: