本文共 395 字,大约阅读时间需要 1 分钟。
Pow(x, n)
public class Solution { public double pow(double x, int n) { if (x == 0) return 0; if (x == 1) return 1; if (n == 0) return 1; if (n == 1) return x; if (x == -1) return (n % 2) == 0 ? 1 : -1; if (n < 0 ) return 1/ pow (x, -n); if (n == 2) return x * x; int halfPow = n/2; return pow(pow(x,halfPow),2) * pow(x, n%2); }}
转载地址:http://lnuni.baihongyu.com/