Leetcode 050 Pow(x, n)(快速幂)

xiaoxiao2021-02-28  42

题目连接:Leetcode 050 Pow(x, n)

解题思路:快速幂裸题。

class Solution { public: double myPow(double x, int n) { long long k = n; if (k < 0) { k = -k, x = 1/x; } double ans = 1; while (k) { if (k&1) ans *= x; x = x*x; k >>= 1; } return ans; } };
转载请注明原文地址: https://www.6miu.com/read-2622295.html

最新回复(0)