题解 | #数值的整数次方#
数值的整数次方
https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00
class Solution { public: double Power(double base, int exponent) { if(base==0.0&&exponent<0) return 0.0; if (exponent==0) return 1; if(exponent==1) return base; if(exponent<0) { base=1/base; exponent=-exponent; } double result=1.0; while (exponent>0) { if(exponent&0x1==1)//位运算效率更高 result*=base; base*=base; exponent=exponent>>1;//位运算效率更高 } return result; } };#剑指OFFER#
剑指offer刷题 文章被收录于专栏
坚持!努力!学习