标签:
题目:
Implement pow(x, n).
题解:
pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。
当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了。
代码如下:
Reference: http://fisherlei.blogspot.com/2012/12/leetcode-powx-n.html
标签:
原文地址:http://www.cnblogs.com/springfor/p/3870929.html