快速幂解决数值的整数次方
数值的整数次方
http://www.nowcoder.com/questionTerminal/1a834e5e3e1a4b7ba251417554e07c00
public class Solution { public double Power(double base, int exponent) { boolean flag = false; if(exponent < 0){ flag = true; exponent = -exponent; } double res = 1; while(exponent > 0){ if((exponent & 1) == 1){ res = res * base; } exponent >>= 1; base *= base; } return flag ? 1 / res : res; } }