Leetcode: Pow(x,n)

来源:互联网 发布:美国租房网站 知乎 编辑:程序博客网 时间:2024/04/29 14:51
class Solution {public:    double pow(double x, int n) {        // Start typing your C/C++ solution below        // DO NOT write int main() function                if(n==0)            return 1;        if(x==0)            return 0;        bool negative=false;        if(n<0){            negative=true;            n=~n;        }        double tmp=x;        double result=1;        while(n){            if(n&1==1)                result*=tmp;            tmp*=tmp;            n>>=1;        }        return negative==false?result:1/result/x;            }};