当前位置:网站首页>Implement pow (x, n) function
Implement pow (x, n) function
2022-07-01 03:36:00 【Enthusiastic citizen Mr. Xue】

This question cannot be used for Loop and multiply , Will timeout . This problem uses the fast power method .
Using binary and decimal conversion method , hold n Open .

In a nutshell ,n One right at a time ,x Multiply yourself every time , When n The rightmost of is 1 when ,res = res * x;
Such as x^10^ Calculate with the above method :
10 The binary of is 1010 Records of the results res = 1
1、1010 On the right is 0, So do not calculate , then 1010 Shift right to 101,x = x*x, Turn into x The square of
2、101 On the far right is 1, Calculation res = res * x; namely res = x The square of ,101 Move right to 10,x = x*x , here x For the initial x Of 4 Power
3、10 On the far right is 0, Don't count , Shift right to 1,x = x*x;x Of the initial value 8 Power
4、1 On the far right is 1, Calculation res = res * x; namely res = x The square of * x Of 8 Power , 1 Move right to 0, x = x * x
5、 end
From the above steps , Is to put x Of 10 To the power of x Of 8 The power and x Square multiplication of
Update every cycle x The value of is x The square of ,x In turn 1 Power 2 Power 4 Power 8 Power 16 Power ..... When n On the far right is 1 when , Then record this time and x The product of the .
public double myPow(double x, int n) {
if(x==0) return 0;
long b = n;
double res = 1;
if(b<0){
x = 1/x;
b = -b;
}
while(b>0){
if((b&1)==1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
Java In the code int32 Variable n \in [-2147483648, 2147483647]n∈[−2147483648,2147483647] , So when n = -2147483648 n=−2147483648 When the n = -n ,n=−n The assignment will be wrong due to out of bounds . The solution is to put n Deposit in long Variable b , Later use b Just operate .
边栏推荐
- Ctfshow blasting WP
- How to achieve 0 error (s) and 0 warning (s) in keil5
- Pathmeasure implements loading animation
- 排序链表(归并排序)
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- 5. [WebGIS practice] software operation - service release and permission management
- ES6解构语法详解
- LeetCode 128最长连续序列(哈希set)
- Split(), split(), slice(), can't you tell?
- Data exchange JSON
猜你喜欢
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

4、【WebGIS实战】软件操作篇——数据导入及处理

The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display

idea插件备份表

雪崩问题以及sentinel的使用

EDLines: A real-time line segment detector with a false detection control翻译

Cookie&Session

后台系统右边内容如何出现滚动条和解决双滚动条的问题
随机推荐
ES6解构语法详解
Subnet division (10)
GCC usage, makefile summary
后台系统右边内容如何出现滚动条和解决双滚动条的问题
Feign remote call and getaway gateway
[us match preparation] complete introduction to word editing formula
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Pyramid scene parsing network [pspnet] thesis reading
The combination of applet container technology and IOT
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Cookie&Session
力扣-两数之和
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Common interview questions for performance test
Pyramid Scene Parsing Network【PSPNet】论文阅读
Pathmeasure implements loading animation
C语言的sem_t变量类型
Keil5中如何做到 0 Error(s), 0 Warning(s).
Detailed explanation of ES6 deconstruction grammar
Split(), split(), slice(), can't you tell?