您的位置:首页 > 其它

[LeetCode] Pow(x, n) 解题报告

2016-01-12 11:07 477 查看
Implement pow(x[/i], n[/i]).
» Solve this problem

[解题思路]
二分法,注意n<0的情况。

[code]1:    double power(double x, int n)
2:    {
3:       if (n == 0)
4:         return 1;
5:       double v = power(x, n / 2);
6:       if (n % 2 == 0)
7:         return v * v;
8:       else
9:         return v * v * x;
10:     }
11:     double pow(double x, int n) {
12:       // Start typing your C/C++ solution below
13:       // DO NOT write int main() function
14:       if (n < 0)
15:         return 1.0 / power(x, -n);
16:       else
17:         return power(x, n);
18:     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: