Pow(x,n) C++ solution


  • 0
    R

    class Solution {
    public:
    double myPow(double x, int n) {
    double res = 1;
    if (n == INT_MIN) {
    if (x == 1) return x;
    else if (x == -1) return -x;
    else return 0;
    } else if (n == INT_MAX) {
    if (x == 1 || x == -1) return x;
    else return 0;
    }
    if(x == 0) return 0;
    else if(x == 1 || n == 0) return 1;
    else
    for(int i = 0; i < fabs(n); ++i)
    res*=x;
    return n > 0 ? res : 1/res;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.