Created
September 9, 2014 04:44
-
-
Save sopherwang/b07e4785c5be091677a0 to your computer and use it in GitHub Desktop.
Pow(x, n) - LeetCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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