当前位置:网站首页>LeetCode 50. Pow(x,n)
LeetCode 50. Pow(x,n)
2022-07-25 11:00:00 【早睡身体好hh】
题目链接:点击这里
先介绍一种精度最高的解法:
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));
}
};
利用倍增思想,快速幂的递归写法:
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);
}
};
边栏推荐
- ArcMap cannot start the solution
- 硬件外设=maixpy3
- JDBC summary
- 什么是全局事件总线?
- Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
- W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
- Getting started with tensorflow
- Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
- Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
- Database integrity -- six constraints learning
猜你喜欢

矩阵的特征值和特征向量

Teach you how to configure S2E as the working mode of TCP server through MCU

MySQL historical data supplement new data

W5500上传温湿度到oneNET平台

WIZnet嵌入式以太网技术培训公开课(免费!!!)

Leetcode sword finger offer 27. image of binary tree

全网显示 IP 归属地,是怎么实现的?

Reinforcement learning (IV)

第4章线性方程组

Attendance system based on w5500
随机推荐
[tree] 100. Same tree
Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
Introduction to shortcut keys in debug chapter
【电子器件笔记5】二极管参数和选型
Filter过滤器解决request请求参数乱码的原理解析
Teach you how to configure S2E as the working mode of TCP client through MCU
矩阵的特征值和特征向量
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States
Reptile foundation I
各种控件==PYQT5
SQL注入 Less18(头部注入+报错注入)
SQL language (V)
Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
Onenet platform control w5500 development board LED light
小微企业智能名片管理小程序
How does the whole network display IP ownership?
Dynamic planning problem 03_ Maximum sub segment sum