Pow(x, n)
https://leetcode.com/problems/powx-n/description/
Thoughts
Code
/*
* @lc app=leetcode id=50 lang=cpp
*
* [50] Pow(x, n)
*/
class Solution {
public:
double myPow(double x, int n) {
if (n == 0) return 1;
double res = myPow(x, n / 2);
res *= res;
if (n % 2 == 0) return res;
if (n < 0) return res / x;
return res * x;
}
};
Analysis
Last updated