当前位置:网站首页>LeetCode 50. Pow(x,n)
LeetCode 50. Pow(x,n)
2022-07-25 11:53:00 【Go to bed early and be healthy HH】
Topic link : Click here 
First, a solution with the highest accuracy is introduced :
x n = e n l n x x^n = e^{nlnx} xn=enlnx
class Solution {
public:
double myPow(double x, int n) {
int sign = 1;
if(x<0 && n&1) sign = -1;
x = abs(x);
return sign*exp(n*log(x));
}
};
Use the idea of multiplication , Fast power recursion :
class Solution {
public:
double fastPow(double x, long long n) {
if (n == 0) return 1.0;
double half = fastPow(x, n/2);
if(n%2==0) return half * half;
else return half * half * x;
}
double myPow(double x, int n) {
long long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
return fastPow(x, N);
}
};
边栏推荐
- Similarity matrix, diagonalization condition
- [electronic device notes 5] diode parameters and selection
- 信号与槽机制==PYQT5
- [MySQL learning 09]
- Leetcode sword finger offer 28. symmetric binary tree
- JS作用域以及预解析
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- brpc源码解析(七)—— worker基于ParkingLot的bthread调度
- W5500 multi node connection
- Risks in software testing phase
猜你喜欢
随机推荐
Plot ==pyqt5
软件测试阶段的风险
Filter过滤器解决request请求参数乱码的原理解析
How does the whole network display IP ownership?
Brpc source code analysis (VI) -- detailed explanation of basic socket
Classification parameter stack of JS common built-in object data types
Linked list related (design linked list and ring linked list)
动态规划问题03_最大子段和
软件缺陷的管理
Wiznet embedded Ethernet technology training open class (free!!!)
JS流程控制
W5500通过上位机控制实现调节LED灯带的亮度
Web APIs (get element event basic operation element)
Leetcode sword finger offer 27. image of binary tree
brpc源码解析(四)—— Bthread机制
flinksql client 连接kafka select * from table没有数据报错,如何解决?
Teach you how to configure S2E as the working mode of TCP client through MCU
小区蔬菜配送的小程序
W5500 adjusts the brightness of LED light band through upper computer control
W5500多节点连接






![[MySQL learning 08]](/img/9e/6e5f0c4c956ca8dc31d82560262013.png)


