Pow(x, n)

Implement pow(x, n).

Solution

public class Solution {
  public double myPow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }

        boolean flag = false;
        if (n < 0) {
            flag = true;
            n = -n;
        }

        double result;
        if (n == 1) {
            result = x;
        } else {

            if (n % 2 == 0) {
                double temp = myPow(x, n / 2);
                result = temp * temp;
            } else {
                result = x * myPow(x, n - 1);
            }
        }

        if (flag) {
            return 1.0 / result;
        } else {
            return result;
        }
    }
}

results matching ""

    No results matching ""