publicclassSolution_16{publicdoublemyPow(double x,int n){if(n ==0|| x ==1)return1;if(n <0)return1.0/myPow(x,-n);if(n %2==0){return Math.pow(myPow(x, n /2),2);}else{return Math.pow(myPow(x, n /2),2)* x;}}}
一般的思路,一个个的乘
classSolution{publicdoublemyPow(double x,int n){if(n ==0)return1.0;if(x ==1)return1.0;if(n>0){double res =1.0;while(n!=0){
res *= x;
n--;}return res;}if(n<0){double res =1.0;while(n!=0){
res *= x;
n ++;}return1.0/res;}return0;}}