Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. // 0ms, 100%
  2. // 8.8MB, 5.11%
  3.  
  4. class Solution {
  5. public:
  6. double ePow(double x, int n, map<int,double> &m) {
  7. double h = 0;
  8. if (m.end() == m.find(n/2)) {
  9. h = m[n/2] = ePow(x, n/2, m);
  10. } else {
  11. h = m[n/2];
  12. }
  13.  
  14. if (isinf(h)) return INFINITY;
  15. if (0 == n%2) return h*h;
  16. if (0 < n) return h*h*x;
  17. return h*h/x;
  18. }
  19. double myPow(double x, int n) {
  20. if (n == 0 || x == 1 || (x == -1 && n%2 == 0)) return 1;
  21. if (x == -1) return -1;
  22. if (n == 1) return x;
  23. if (n == -1) return 1.0/x;
  24.  
  25. map <int, double> m;
  26. m[1] = x;
  27. m[-1] = 1.0/x;
  28. return ePow(x, n, m);
  29. }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement