50. Pow(x,n)

解法1:

很简洁的一个写法,用了divide&conquer的思想
C++

1

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
public double myPow(double x, int n) {
if (n == 0) {
return 1;
}
double temp = myPow(x, n / 2);
if (n % 2 == 0) {
return temp * temp;
} else {
if (n > 0) {
return temp * temp * x;
} else {
return temp * temp / x;
}
}
}
}