50. Pow(x, n)

Implement pow(xn).

 1 public class Solution {
 2     public double myPow(double x, int n) {
 3         if(n==0) return 1;
 4         if(n<0){
 5             if(n==Integer.MIN_VALUE){
 6                 x = 1/x;
 7                 n = n+1;
 8                 n = -n;
 9                 return x*x*myPow(x*x,n/2);
10             }
11             x = 1/x;
12             n = -n;
13         }
14         return n%2==0?myPow(x*x,n/2):x*myPow(x*x,n/2);
15     }
16 }

 

posted @ 2017-05-27 02:58  CodesKiller  阅读(127)  评论(0编辑  收藏  举报