当前位置:网站首页>Leetcode 16. Numerical integral power (power + fast recursive/iteration)
Leetcode 16. Numerical integral power (power + fast recursive/iteration)
2022-08-03 20:12:00 【Luna who can program】
实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn).不得使用库函数,同时不需要考虑大数问题.
示例 1:
输入:x = 2.00000, n = 10
输出:1024.00000
示例 2:
输入:x = 2.10000, n = 3
输出:9.26100
示例 3:
输入:x = 2.00000, n = -2
输出:0.25000
解释:2-2 = 1/22 = 1/4 = 0.25
提示:
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104
思路:
快速幂+递归
class Solution {
public:
double myQuick(double x,long long k){
if(k==0)
return 1.0;
double y=myQuick(x,k/2);
return k%2==0 ? y*y : y*y*x;
}
double myPow(double x, int n) {
long long N=n;
return N>0 ? myQuick(x,N) : 1.0/myQuick(x,-N);
}
};
快速幂+迭代:
class Solution {
public:
double myPow(double x, int n) {
if(n==0)
return 1.0;
if(x==0)
return 0;
double ans=1.0;
long long b=n; //因为intWhen it is a positive number, the complement is itself,If it is negative number's complement,Need to add after changing the sign1,如果传进来的是(-2)的32次方,It will be exceeded after it is reversedint,所以要用long longto expand the type
if(n<0){
x=1/x; //Here is the direct reciprocal first,Calculate the countdown againn次方
b=-b;
}
while(b>0){
if(b&1==1) //如果b最后一位为1,Just multiply in the final result,是0It doesn't count in the results
ans*=x;
x*=x;
b>>=1; //The right shift operator can be thought of as deleting the last bit,Remove the last one in the low order,高位正数补0,负数补1
}
return ans;
}
};
You can see the details of the left shift and right shift operations:Algorithms for left shift and right shift
边栏推荐
- 演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
- EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
- leetcode 1837. K 进制表示下的各位数字总和
- Kubernetes资源编排系列之三: Kustomize篇 作者 艄公(杨京华) 雪尧(郭耀星)
- Statistical machine learning 】 【 linear regression model
- 149. The largest number on a straight line, and check the set
- Golang死信队列的使用
- pytorch框架实现老照片修复功能详细演示(GPU版)
- leetcode 2119. 反转两次的数字
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
猜你喜欢
Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
利用 rpush 和 blpop 实现 Redis 消息队列
[email protected] 610/[email protected] 594/Alexa 56"/>
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
node版本切换工具NVM以及npm源管理器nrm
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
若依集成easyexcel实现excel表格增强
单调栈及其应用
ESP8266-Arduino编程实例-WS2812驱动
随机推荐
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
leetcode 899. 有序队列
开源教育论坛| ChinaOSC
ES6简介及let、var、const区别
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
深入理解JVM-内存结构
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
leetcode 072. Finding Square Roots
PHP according to the longitude and latitude calculated distance two points
glusterfs 搭建使用
CSDN帐号管理规范
Use ControlTemplate or Style from resource file in WPF .cs and find the control
力扣59-螺旋矩阵 II——边界判断
【微信小程序2】事件传参与数据同步[03]
Alexa染料标记RNA核糖核酸|RNA-Alexa 514|RNA-Alexa 488|RNA-Alexa 430
In-depth understanding of JVM-memory structure
数据驱动的软件智能化开发| ChinaOSC
Auto.js实现朋友圈自动点赞
leetcode 072. 求平方根