方法
算法思想:引用 https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/solution/mian-shi-ti-16-shu-zhi-de-zheng-shu-ci-fang-kuai-s/
时间复杂度:O(log以二为底n)
空间复杂度:O(1)
边界条件:防止越界,将n存入long类型中
补充知识:
class Solution {
public double myPow(double x, int n) {
if(x==0) return 0;
long b=n;
double res=1.0; //第一次初始
if(b<0) { //小于,转换
x=1/x;
b=-b;
}
while(b>0){ //大于正常操作
if((b&1)==1) res*=x; //只要为奇数,就归入res一个x
x*=x; //双倍
b>>=1; //除以2
}
return res;
}
}