当前位置:网站首页>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);
}
};
边栏推荐
- 用 Redis 做一个可靠的延迟队列
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- JVM performance tuning methods
- Summary of combination problems of Li Kou brush questions (backtracking)
- JDBC summary
- brpc源码解析(八)—— 基础类EventDispatcher详解
- There is no sound output problem in the headphone jack on the front panel of MSI motherboard [solved]
- SQL language (V)
- Common web attacks and defense
- W5500 upload temperature and humidity to onenet platform
猜你喜欢
随机推荐
【mysql学习08】
W5500 adjusts the brightness of LED light band through upper computer control
基于W5500实现的考勤系统
brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制
brpc源码解析(四)—— Bthread机制
Varest blueprint settings JSON
WIZnet嵌入式以太网技术培训公开课(免费!!!)
Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
Teach you how to configure S2E as the working mode of TCP client through MCU
小微企业智能名片管理小程序
小区蔬菜配送的小程序
Review in the middle of 2022 | understand the latest progress of pre training model
第一个C语言程序(从Hello World开始)
Dynamic planning question 05_ Missile interception
Linked list related (design linked list and ring linked list)
各种控件==PYQT5
Menu bar + status bar + toolbar ==pyqt5
教你如何通过MCU配置S2E为TCP Server的工作模式
Make a reliable delay queue with redis
世界上最高效的笔记方法(改变你那老版的记笔记方法吧)




![[leetcode brush questions]](/img/86/5f33a48f2164452bc1e14581b92d69.png)




