这道题需要注意的是long long

    1. class Solution {
    2. public:
    3. double myPow(double x, int n) {
    4. if(n == 0) return 1;
    5. if(x == 1) return 1;
    6. bool bMinus = false;
    7. long long iExp = n;
    8. if(n < 0)
    9. {
    10. bMinus = true;
    11. iExp = -iExp;
    12. }
    13. double res = 1;
    14. while(iExp)
    15. {
    16. if(iExp & 1) res *= x;
    17. x *= x;
    18. iExp >>= 1;
    19. }
    20. if(bMinus) return 1.0/res;
    21. else return res;
    22. }
    23. };