leetcode-----50. Pow(x, n)

  • 链接:https://leetcode-cn.com/problems/powx-n/

代码

class Solution {
public:
    #define LL long long
    double myPow(double x, int n) {
        double ans = 1, p = x;
        LL t = abs((LL)(n));
        for (; t; t >>= 1) {
            if (t & 1) {
                ans = ans * p;
            }
            p = p * p;
        }
        return n > 0 ? ans : 1 / ans;
    }
};

你可能感兴趣的:(leetcode-----50. Pow(x, n))