Using recursion by dividing string by 2


#1

int Solution::power(string A) {
if(A==“2”) return 1;
int n= A.length();
if((A[n-1]-‘0’)%2==1) return 0;
int carry=0;
string ans="";
bool b=true;
for(int i=0; i<n; i++){
int a = A[i]-‘0’;
a += carry*10;
carry = a%2;
a /= 2;
if(!b || a!=0){
ans += a + ‘0’;
b=false;
}
}
return power(ans);
}