其他分享
首页 > 其他分享> > leetcode:位操作

leetcode:位操作

作者:互联网

文章目录

01 数值的整数次方

在这里插入图片描述
解法
在这里插入图片描述

class Solution {
public:
    double myPow(double x, int n) {
        int pow = abs(n);
        if(pow == 0)
            return 1.0;
        //将n转为为二进制来考虑问题
        double res = 1.0;
        while(pow > 0)
        {
            if(pow & 1)
            {
                res *= x;
            }
            x *= x;
            pow >>= 1; //右移等于除2
        }
        //如果是负数
        if(n < 0)
        {
            res = 1/ res;
        }
        return res;
    }
};

标签:位操作,01,1.0,int,double,res,pow,leetcode
来源: https://blog.csdn.net/weixin_42376458/article/details/116902021