描述 Implement pow(x, n).
分析 二分法,xn = xn/2 × xn/2 × xn%2
代码
class Solution {public: double myPow(double x, int n) { if (n < 0) return 1.0 / power(x, -n); else return power(x, n); }PRivate: double power(double x, int n) { if (n == 0) return 1; double v = power(x, n / 2); if (n % 2 == 0) return v * v; else return v * v * x; }};新闻热点
疑难解答