50. Pow(x, n)C++

it2022-05-05  113

double myPow(double x, int n) { long long N = n; if (N < 0) { x = 1 / x; N = -N; } double ans = 1; double current = x; for (long long i = N; i>0 ; i /= 2) { if ((i % 2) == 1) { ans = ans * current; } current = current * current; } return ans; }

最新回复(0)