classSolution{publicdoublemyPow(double x,int n){long nLong =(long) n;// Ignore negative conditiondouble res =helper(x,Math.abs(nLong));// Consider negative conditionreturn(nLong >=0)? res :1/ res;}privatedoublehelper(double x,long n){// Base casesif(x ==0)return0;if(n ==0)return1;// Divide for optimisationdouble res =helper(x, n /2);// Due to divide, multiply them will do the work
res = res * res;// Odd n should multiply againreturn(n %2==1)? x * res : res;}}