implemented by binary search tree in C++, can be implemented by more space


  • 0
    H

    class Solution {
    private:
    int count = 1;
    double twopow(double x, int m){
    count = 1;
    double xmul = x;
    while(count <= m / 2){
    xmul = xmul * xmul;
    count = count * 2;
    }
    return xmul;
    }
    public:
    double myPow(double x, int n) {
    if(n == 0) return 1;
    int m = n;
    if(n < 0 && m != INT_MIN) { x = (1.0 / x ); m = -n ;}
    if( n == INT_MIN) { m = m +1; m = -m; x = 1.0 / x; }

        int k = 1;
         double xm = x;
        int pown = m -1;
        while(pown) {
           xm = xm * twopow(x, pown);
           k = k + count; 
           pown = m - k; 
        }
      if(n == INT_MIN) xm = x * xm;
       return xm; 
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.