Pow(x, n) (Pow(x, n))

 

思路:

// @Title: Pow(x, n) (Pow(x, n))
// @Author: qisiii
// @Date: 2024-05-10 23:32:07
// @Runtime: 0 ms
// @Memory: 41.6 MB
// @comment: 
// @flag: 
class Solution {
    public double myPow(double x, int n) {
        if(n>=0){
            return quickRow(x,n);
        }else{
            return 1/quickRow(x,n);
        }
    }

    public double quickRow(double x,int n){
        if(x==1){
            return x;
        }
        if(n==0){
            return 1.0;
        }
        double half=quickRow(x,n/2);
        return n%2==0?half*half:half*half*x;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18