50. Pow(x, n)
Problem's Link
----------------------------------------------------------------------------
Mean:
略.
analyse:
快速幂.
Time complexity: O(N)
view code
class
Solution
{
public :
double myPow( double n , int k)
{
double res;
bool flag = 0;
unsigned m = k;
if( k < 0)
{
n = 1. /n;
m =- m;
}
double ans = 1;
while( m)
{
if( m & 1)
ans *=n;
m >>= 1 ,n *=n;
}
return ans;
}
};
{
public :
double myPow( double n , int k)
{
double res;
bool flag = 0;
unsigned m = k;
if( k < 0)
{
n = 1. /n;
m =- m;
}
double ans = 1;
while( m)
{
if( m & 1)
ans *=n;
m >>= 1 ,n *=n;
}
return ans;
}
};