当前位置:网站首页>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 .
边栏推荐
- Research on target recognition and tracking based on 3D laser point cloud
- 排序链表(归并排序)
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- Bilinear upsampling and f.upsample in pytorch_ bilinear
- Learning notes for introduction to C language multithreaded programming
- Detailed list of errors related to twincat3 ads of Beifu
- [深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
- pytorch训练深度学习网络设置cuda指定的GPU可见
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
- Hal library operation STM32 serial port
猜你喜欢

过滤器 Filter

Bilinear upsampling and f.upsample in pytorch_ bilinear

数据库中COMMENT关键字的使用

详解Spark运行模式(local+standalone+yarn)
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

Let's just say I can use thousands of expression packs

不用加减乘除实现加法

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear

Take you through a circuit board, from design to production (dry goods)

Avalanche problem and the use of sentinel
随机推荐
TEC: Knowledge Graph Embedding with Triple Context
torch. histc
Detailed list of errors related to twincat3 ads of Beifu
Gorilla/mux framework (RK boot): RPC error code design
Valid brackets (force deduction 20)
在线公网安备案保姆级教程【伸手党福利】
Edge Drawing: A combined real-time edge and segment detector 翻译
leetcode 1482 猜猜看啊,这道题目怎么二分?
Hal library operation STM32 serial port
Server rendering technology JSP
终极套娃 2.0 | 云原生交付的封装
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
pytest-fixture
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
用小程序的技术优势发展产业互联网
雪崩问题以及sentinel的使用
5、【WebGIS实战】软件操作篇——服务发布及权限管理
Force buckle - sum of two numbers
Analyze datahub, a new generation metadata platform of 4.7K star
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear