当前位置:网站首页>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 .
边栏推荐
- Edlines: a real time line segment detector with a false detection control
- Develop industrial Internet with the technical advantages of small programs
- 过滤器 Filter
- ECMAScript 6.0
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- split(),splice(),slice()傻傻分不清楚?
- Home online shopping project
- md5sum操作
- 网页不能右键 F12 查看源代码解决方案
猜你喜欢

Bilinear upsampling and f.upsample in pytorch_ bilinear
![4. [WebGIS practice] software operation chapter - data import and processing](/img/5a/b86e0538660f27c809cf429053a06c.png)
4. [WebGIS practice] software operation chapter - data import and processing

Develop industrial Internet with the technical advantages of small programs

Feign远程调用和Getaway网关

终极套娃 2.0 | 云原生交付的封装

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

Overview of EtherCAT principle

排序链表(归并排序)

idea插件备份表

Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
随机推荐
复习专栏之---消息队列
Develop industrial Internet with the technical advantages of small programs
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
idea插件备份表
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
How to achieve 0 error (s) and 0 warning (s) in keil5
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
Edge drawing: a combined real-time edge and segment detector
Stop saying that you can't solve the "cross domain" problem
gcc使用、Makefile总结
JUC学习
Leetcode 1482 guess, how about this question?
Ouc2021 autumn - Software Engineering - end of term (recall version)
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
雪崩问题以及sentinel的使用
Depth first traversal of C implementation Diagram -- non recursive code
Leetcode:829. Sum of continuous integers