Leet - 50

Having fun on Leetcode with C++, Java, Python & GO

Problem 50. Pow(x, n)

My attempt(s)

Code

C++

1
2
3
4
5
6
class Solution {
public:
    double myPow(double x, int n) {
        return pow(x,n);
    }
};

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public double myPow(double x, int n) {
        double cnt=x;
        boolean nrev=true;
        if (n<0){
           nrev=false;
            n*=-1;
        }
        else if (n==0)
            return 1.00000;
        else if (x==1)
            return 1.00000;
        for (int i=1; i<n; i++)
                cnt=cnt*x;
        if (nrev)
            return cnt;
        else
            return 1/cnt;
        
    }
}

Explanation of idea

C++ version: lol Java version: Not funtionable solution

Good solution ref.

Code

Analysis

Licensed under CC BY-NC-SA 4.0