Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 665 Bytes

powx-n.md

File metadata and controls

32 lines (26 loc) · 665 Bytes

Solution

    class Solution {
    public:
        double pow(double x, int n) {
            if(n == 0)
                return 1;
            if(n == 1)
                return x;
            double temp = pow(x, n / 2);
            if(n % 2 == 0)
                return temp * temp;
            else 
                return temp * temp * x;
        }
        double myPow(double x, int n) {
            if(n >= 0)
                return pow(x, abs(n));
            else 
                return 1 / pow(x, abs(n));
        }
    };