classSolution{publicdoublemyPow(double x,int n){long N = n;if(N <0){
x =1/ x;
N =-N;}double ans =1;double current_product = x;for(long i = N; i >0; i /=2){if((i %2)==1){
ans = ans * current_product;}
current_product = current_product * current_product;}return ans;}}
作者:LeetCode
链接:https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode第50题:Pow(x,n)(中等)题目:实现 pow(x, n) ,即计算 x 的 n 次幂函数。解法一:暴力解法,会超出时间限制。class Solution { public double myPow(double x, int n) { double ans=1; int k=0; if(x==0.0000) ...