leetcode 剑指 Offer 16. 数值的整数次方

2021-02-03  本文已影响0人  flood_d

0.code

class Solution {
    public double myPow(double x, int n) {
        if(x==0){
            return 0;
        }
        double res = 1.0;
        long b = n;
        if(b<0){
            b = -b;
            x = 1/x;
        }
        while(b>0){
            if((b&1)==1){
                res = res*x;
            }
            x = x*x;
            b=b>>1;
        }
        return res;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读