Here is my solution with O(1)


  • -12
    C

    class Solution {
    public:
    double pow(double x, int n) {
    int flag = 0;
    if(n==0)return 1;
    if(x==1)return 1;
    if(x==-1)return n%2==1? -1:1;
    x<0?flag = 1:flag = 0;
    if (flag == 1) {
    x = -x;
    }
    if(n<0){
    x = 1/x;
    n = -n;
    }
    return flag&(n%2==1)? -exp(nlog(x)): exp(nlog(x));
    }
    };

    Still with some problems with bad formatting..... Trying to do some improvement


  • 0
    S

    The exp() is not of O(1). Bad trick.


    edit: Oh wait... exp() is implemented with Talyor series in O(1)?


Log in to reply
 

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