---
# System prepended metadata

title: 'LeetCode 50: Pow(x, n) Solution'

---

# LeetCode 50: Pow(x, n) Solution
[LeetCode 50: Pow(x, n)](https://leetcode.com/problems/powx-n/description/)
Exponentiation by squaring
```clike=
class Solution {
public:
    double myPow(double x, long long n) {
        if(n == 0 || x == 1) return 1;

        if(n < 0) {
            return 1.0 / myPow(x, -n);
        }

        double temp = 1;
        if(n % 2 == 1) temp = x;

        double rec = myPow(x, n / 2);

        return  rec * rec * temp;
    }
};
```