Skip to content
Discussion options

You must be logged in to vote
class Solution {
    public double myPow(double x, int n) {
        if(n<0){
            return 1/pow(x,-n);
        } else 
            return pow(x,n);
    }
    double pow(double x, int n){
        if(n == 0) {
            return 1;
        }
        double v = pow(x, n/2);
        if(n%2==0) {
            return v*v;
        } else {
            return v*v*x;
        }
    }
}

快速幂 + 递归,「快速幂算法」的本质是分治算法。

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by taotie6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants