其他分享
首页 > 其他分享> > 50. Pow(x, n)

50. Pow(x, n)

作者:互联网

package leetcode;

public class demo_50 {
    public double myPow(double x, int n) {
        if(n==0) {return 1.0;}
        if(n<0) {
           return 1.0/backtrack(x, -n);
        }
        return backtrack(x, n);
    }
    //二分法
    public double backtrack(double x,int n) {
        double pow;
        if(n==0) {
            return 1.0;
        }
        pow=backtrack(x, n/2);
        if(n%2==0) {
            return pow*pow;
        }
        else {
            return x*pow*pow;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        demo_50 d50=new demo_50();
        System.out.println(d50.myPow(2.00000,10));
    }

}

 

标签:myPow,return,package,Pow,50,double,public
来源: https://www.cnblogs.com/Yshun/p/14823583.html