当前位置:网站首页>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 .
边栏推荐
- Ridge regression and lasso regression
- Feature Pyramid Networks for Object Detection论文理解
- 5、【WebGIS实战】软件操作篇——服务发布及权限管理
- Asgnet paper and code interpretation 2
- 不用加减乘除实现加法
- Gorilla/mux framework (RK boot): RPC error code design
- GCC usage, makefile summary
- pytorch训练深度学习网络设置cuda指定的GPU可见
- 排序链表(归并排序)
- The shell script uses two bars to receive external parameters
猜你喜欢

复习专栏之---消息队列

Basic concepts of database

ctfshow爆破wp
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

服务器渲染技术jsp

idea插件备份表

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

pytorch训练深度学习网络设置cuda指定的GPU可见

Edlines: a real time line segment detector with a false detection control

线程数据共享和安全 -ThreadLocal
随机推荐
Promise中finally的用法
Edge Drawing: A combined real-time edge and segment detector 翻译
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
Stop saying that you can't solve the "cross domain" problem
力扣-两数之和
Depth first traversal of C implementation Diagram -- non recursive code
Ctfshow blasting WP
详解Spark运行模式(local+standalone+yarn)
Cookie&Session
[nine day training] content III of the problem solution of leetcode question brushing Report
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
About the application of MySQL
Cookie&Session
雪崩问题以及sentinel的使用
multiple linear regression
Include() of array
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
Avalanche problem and the use of sentinel
Force buckle - sum of two numbers