Skip to content

Instantly share code, notes, and snippets.

@sopherwang
Created September 9, 2014 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sopherwang/b07e4785c5be091677a0 to your computer and use it in GitHub Desktop.
Save sopherwang/b07e4785c5be091677a0 to your computer and use it in GitHub Desktop.
Pow(x, n) - LeetCode
public double pow(double x, int n)
{
if (n == 0)
{
return 1;
}
if (n < 0)
{
return 1.0 / power(x, -n);
}
else
{
return power(x, n);
}
}
private double power(double x, int n)
{
if (n == 0)
{
return 1;
}
double tem = power(x, n / 2);
if (n % 2 == 0)
{
return tem * tem;
}
else
{
return tem * tem * x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment