Implement pow(x, n).
Example 1:
Input: 2.00000, 10 Output: 1024.00000
Example 2:
Input: 2.10000, 3 Output: 9.26100
实现x^n。这里要注意的是n为负数的情况。n为负数时也可以将n转成正数,这样x=1/x。。代码用了另一种方法,见代码。
class Solution { public double myPow(double x, int n) { if(n==0) return 1; double t=myPow(x,n/2);
//这里用!=0,因为n为负数时,余数也可能是-1 if(n%2!=0) return n<0?1/x*t*t:x*t*t; else return t*t; } }