Pow(x, n)

Pow(x, n)
 1 public class Solution {
 2     public double pow(double x, int n) {
 3         if(x==0||x==1) return x;
 4         if(n<0)return 1/helper(x,-1*n);
 5         else return helper(x,n);
 6     }
 7     public double helper(double x,int n){
 8         if(n==0) return 1;
 9         double res = helper(x,n/2);
10         if(n%2==0) return res*res;
11         else return res*res*x;
12     }
13 }
View Code

Pow(x, n)

上一篇:Path Sum


下一篇:Permutation Sequence