当前位置:网站首页>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);
}
};
边栏推荐
- 【电子器件笔记5】二极管参数和选型
- 已解决The JSP specification requires that an attribute name is preceded by whitespace
- [MySQL learning 08]
- W5500 upload temperature and humidity to onenet platform
- The principle analysis of filter to solve the request parameter garbled code
- How does the whole network display IP ownership?
- 基于W5500实现的考勤系统
- Fillet big killer, use filter to build fillet and wave effect!
- Arrays in JS
- W5500通过上位机控制实现调节LED灯带的亮度
猜你喜欢
随机推荐
软件缺陷的管理
"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
[leetcode brush questions]
【leetcode刷题】
JS作用域以及预解析
brpc源码解析(二)—— brpc收到请求的处理过程
Various controls ==pyqt5
JVM performance tuning methods
JS中的函数
RedisUtil
Linked list related (design linked list and ring linked list)
WIZnet W5500系列培训活动之“MQTT协议讲解和实践(接入OneNET)”
Web APIs (get element event basic operation element)
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
布局管理==PYQT5
toString()与new String()用法区别
[MySQL learning 08]
Dynamic planning problem 03_ Maximum sub segment sum
贪心问题01_活动安排代码分析
SQL injection LESS18 (header injection + error injection)




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



![[USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)](/img/ce/534834c53c72a53fd62ff72a1d3b39.png)
