-
Notifications
You must be signed in to change notification settings - Fork 0
/
191225-1.cpp
47 lines (45 loc) · 972 Bytes
/
191225-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode-cn.com/problems/powx-n/
#include <cstdio>
#include <climits>
#include <unordered_map>
using namespace std;
class Solution {
public:
double myPow(double x, int n) {
unordered_map<int, double> cache;
return calcPow(x, n, cache);
}
private:
double calcPow(double x, int n, unordered_map<int, double>& cache) {
auto it = cache.find(n);
if (it != cache.end()) return it->second;
double r;
if (n == 0) {
r = 1;
} else if (n < 0) {
if (n != INT_MIN) {
r = 1 / calcPow(x, -n, cache);
} else {
r = 1 / calcPow(x, INT_MAX, cache) / x;
}
} else if (n == 1) {
r = x;
} else {
r = calcPow(x, n / 2, cache);
r *= r;
if (n % 2 == 1) {
r *= x;
}
}
cache[n] = r;
return r;
}
};
int main()
{
Solution s;
printf("%.5f\n", s.myPow(2.00000, 10)); // answer: 1024.00000
printf("%.5f\n", s.myPow(2.10000, 3)); // answer: 9.26100
printf("%.5f\n", s.myPow(2.00000, -2)); // answer: 0.25000
return 0;
}