0050.cpp (237B)
1 class Solution { 2 public: 3 double myPow(double x, int n) { 4 if (n == 0) return 1; 5 if (n < 0) return 1 / x * myPow(1 / x, -(n + 1)); 6 return (n % 2 == 0) ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2); 7 } 8 };