class Solution {
public:
    double powPositive(double x, int n){
        if(n == 0) return 1;
        if(n == 1) return x;
        
        double tmp;
        if(n%2 == 0){
            tmp = powPositive(x, n/2);
            return tmp*tmp;
        }
        
        tmp = powPositive(x, n/2);
        
        return tmp*tmp*x;
    }
    
    double pow(double x, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n >= 0) return powPositive(x,n);
        return 1/powPositive(x,-n);
        
    }
};


相关文章:

  • 2022-03-08
  • 2021-11-12
  • 2021-11-23
  • 2021-10-24
  • 2021-12-24
  • 2021-10-15
猜你喜欢
  • 2021-12-25
  • 2021-11-19
  • 2022-01-30
  • 2022-01-19
  • 2021-10-20
相关资源
相似解决方案