// 不需要遍历n次,时间复杂度为O(logn) int pow(int m, int n) { int sum = 1; int tmp = m; while(n != 0) { if(n & 1 == 1) { // 该位是1,则乘上该位对应的幂次 sum *= tmp; } tmp *= tmp; n >> 1; } return sum; }
评论(1)