LeetCode-Pow


class Solution { public: double pow(double x, int n) { if(n==1)return x; else if(n==-1)return 1/x; else if(n==0)return 1; else{ bool flag=n>0; if(!flag)n=-n; double ret=1; int bit=0x40000000; for(int i=0;i<31;i++){ if(n&bit){ ret=ret*ret*x; } else{ ret=ret*ret; } bit/=2; } if(!flag)return 1/ret; return ret; } // Start typing your C/C++ solution below // DO NOT write int main() function } };
原文地址:https://www.cnblogs.com/superzrx/p/3259566.html