当前位置:网站首页>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);
}
};
边栏推荐
- SQL language (II)
- W5500 multi node connection
- 模型部署简述
- SQL language (4)
- 2022 年中回顾|一文看懂预训练模型最新进展
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- 第4章线性方程组
- 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)
- 教你如何通过MCU将S2E配置为UDP的工作模式
- Shell - Chapter 6 exercise
猜你喜欢
随机推荐
全网显示 IP 归属地,是怎么实现的?
Chapter 4 linear equations
JS作用域以及预解析
The principle analysis of filter to solve the request parameter garbled code
Definition of information entropy
Common linear modulation methods based on MATLAB
贪心问题01_活动安排问题
Review in the middle of 2022 | understand the latest progress of pre training model
Getting started with tensorflow
Summary of combination problems of Li Kou brush questions (backtracking)
JS数据类型以及相互转换
Information management system for typical works of urban sculpture (picture sharing system SSM)
Dynamic planning problem 03_ Maximum sub segment sum
Database integrity -- six constraints learning
toString()与new String()用法区别
小区蔬菜配送的小程序
Greedy problem 01_ Activity arrangement code analysis
JS中的数组
菜单栏+状态栏+工具栏==PYQT5
【mysql学习09】









