【Leetcode】Pow(x, n)

题目链接:https://leetcode.com/problems/powx-n/
题目:

Implement pow(xn).

思路:

easy。上课例题= =

算法:

public double myPow(double x, int n) {  
    if(n<0)  
     return 1.0/(myPow2(x,-n));  
    else  
     return myPow2(x,n);  
   
}  
public double myPow2(double x,int n){  
 if(n==0)  
     return 1;  
double r=  myPow2(x,n/2);  
if(n%2==0){  
    return r*r;  
}else{  
    return r*r*x;  
}  
}  


你可能感兴趣的:(【Leetcode】Pow(x, n))