50.实现幂函数

xiaoxiao2021-02-28  103

问题描述:

Implement pow(x, n).

参考答案:

class Solution { public: double myPow(double x, int n) { double ans = 1; unsigned long long p; if (n < 0) { p = -n; x = 1 / x; } else { p = n; } while (p) { if (p & 1) ans *= x; x *= x; p >>= 1; } return ans; } };

性能:

转载请注明原文地址: https://www.6miu.com/read-81476.html

最新回复(0)