快速幂Java(leetcode 50 计算 Pow(x, n)

class Solution {
    public double myPow(double x, int n) {
        double res = 1;
        for(int i = n; i != 0; i /= 2) {
            if((i & 1) == 1) res *= x;
            x *= x;
        }
        return n < 0 ? 1 / res : res;
    }
}
posted @ 2021-01-18 23:19  SteveYu  阅读(124)  评论(0编辑  收藏  举报