LeetCode: Pow(x, n)

Pow(x, n)

Implement pow(xn).

 

 1 public class Solution {
 2     private double power(double x, int n) {
 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     }
12  
13     public double pow(double x, int n) {
14         if (n < 0) {
15             return 1 / power(x, -n);
16         } else {
17             return power(x, n);
18         }
19     }
20 }

 

posted @ 2014-03-02 20:42  andyqee  阅读(93)  评论(0编辑  收藏  举报