50. Pow(x, n)(LeetCode中等)(快速幂板子)

50. Pow(x, n)
n很大,故而使用快速幂

class Solution {
public:
    double myPow(double x, int n) {    
        bool is_minus = n < 0;
        double res = 1;
        for(long long k = abs((long long)(n)); k; k >>= 1){
            if(k & 1) res *= x;
            x *= x;
        }
        if(is_minus) res = 1 / res;

        return res;

    }
};
posted @ 2025-03-14 16:48  awei040519  阅读(13)  评论(0)    收藏  举报