Pow(x, n)

Implement pow(x, n).

解题

直接顺序求解,时间复杂度O(N)

public class Solution {
    /**
     * @param x the base number
     * @param n the power number
     * @return the result
     */
    public double myPow(double x, int n) {
        // Write your code here
        if(x == 0)
            return 0;
        if(n == 0)
            return 1.0;
        if( n<0)
            return 1.0/(myPow(x,-n));
        double res = x;
        while(n>1){
            res*=x;
            n--;
        }
        return res;
    }
}
Java Code

分类:

技术点:

相关文章: