Easiest c++ soln using log2()


#1

int Solution::power(string A) {

long double a= stold(A);
if(a==1)
{
    return 0;
}

if(fmod(log2(a),1)==0)
{
    return 1;
}


return 0;

}


#3

it won’t pass if the string is 100 character’s or more in size

my idea is to use multiplication don’t invent division in the string is there is no need

because we have already done some work finding multiplication in strings in the last question’s

string go( string A , string B )
{
int M = A.size(); 
int N = B.size(); 

string res(M+N,'0');

for( int i = M-1 ; i >= 0 ; i-- )
for( int j = N-1 ; j >= 0 ; j-- )
{
    int a = A[i]-'0';
    int b = B[j]-'0';
    
    int c = res[i+j+1]-'0';
    
    int t = c + a*b ; 
    
    res[i+j+1] = t%10+'0';
    res[i+j] += t/10; 
}

for( int i = 0 ; i < res.size() ; i++ )
if( res[i] != '0' )return res.substr(i);

return "0";
}




int Solution::power(string A) {

string t = "2"; 

while( t.size() <= A.size() )
{
    if( A == t )return 1 ; 
    t = go( t , "2" ); 
}
return 0 ; 
}