当前位置:网站首页>50. Pow(x, n)-快速幂
50. Pow(x, n)-快速幂
2022-06-25 06:40:00 【Mr Gao】
50. Pow(x, n)
实现 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
解题代码如下,快速幂,一般是以一个2为底数的一个快速乘积算法:
double myPow(double x, int n){
if (n == 0)
return 1; // 递归出口
if (n == 1)
return x; // 递归出口
// if (n < 0)
// return 1 / myPow(x, -n); // 此处n会越界
if (n == -1)
return 1 / x; // 只定义递归出口即可
if (n % 2 != 0)
return x * myPow(x, n - 1);
else
return myPow(x * x, n / 2);
}
边栏推荐
- C Getting Started tutorial
- realsense d455 semantic_slam实现语义八叉树建图
- Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
- This year, I graduated
- Cglib dynamic proxy
- 微信小程序开通客服消息功能开发
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
- STL tutorial 4- input / output stream and object serialization
- Domestic MCU perfectly replaces STM chip model of Italy France
- 【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
猜你喜欢
![Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]](/img/ea/3eba7e4a433b0c501f9b207641dc6a.jpg)
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]

Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing

Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer

【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)

【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
![[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)](/img/2b/4495a6cd41a2dd4e7a20ee60b398c9.png)
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)

三年营收连续下滑,天地壹号困在醋饮料里

Path planner based on time potential function in dynamic environment
![[leetcode] two num · sum of two numbers](/img/9e/79c9deae9f853a265b5b536998b655.png)
[leetcode] two num · sum of two numbers

My debut is finished!
随机推荐
微信小程序入门记录
[Batch dos - cmd Command - Summary and Summary] - External Command - cmd Download Command, wget Command
What is the difference between norflash and nandflash
CGLIB动态代理
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
Application of point cloud intelligent drawing in intelligent construction site
Zhugeliang vs pangtong, taking distributed Paxos
Domestic MCU perfectly replaces STM chip model of Italy France
微信小程序开通客服消息功能开发
This year, I graduated
smartBugs安装小问题总结
基于激光雷达的林业调查常用术语及含义锦集
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]
Summary of small problems in smartbugs installation
STL教程4-输入输出流和对象序列化
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
npm install 报错 : gyp ERR! configure error
点云智绘在智慧工地中的应用
How to use printf of 51 single chip microcomputer
“空间转换”显著提升陡崖点云的地面点提取质量