Implement pow(xn).

 1 class Solution {
 2 public:
 3     double pow(double x, int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (0==n) return 1.0;
 7         if (1==n) return x;
 8         
 9         int k = abs(n);
10         int remainder = k % 2;
11         
12         double result = 1;
13         
14         result = pow(x, k/2);
15         result = result*result*(1==remainder?x:1);
16         
17         if (n>0)
18             return result;
19         else
20             return 1.0/result; 
21     }
22 };
我的答案

相关文章:

  • 2021-12-07
  • 2021-08-18
  • 2021-05-31
  • 2021-07-22
  • 2021-07-06
  • 2022-01-01
猜你喜欢
  • 2021-08-23
  • 2022-12-23
  • 2021-09-24
  • 2021-09-15
  • 2022-01-09
相关资源
相似解决方案