当前位置:网站首页>2022 年有哪些流行的技术?
2022 年有哪些流行的技术?
2022-07-26 16:37:00 【东方睡衣】
实现 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
解题思路-分情况讨论
本题可以分几种情况讨论:\
- 如果
x = 1,那么无论n的值是多少,结果都是1 - 如果
n = 0,那么无论x的值是多少,结果都是1 - 如果
n = 1,那么无论x的值是多少,结果都是x - 如果
x = -1,那么如果n是偶数,结果是1,否则结果是-1 - 如果
n > 0,则结果为1 *= xn次 - 如果
n < 0,则结果为1 /= xn次
代码实现
var myPow = function(x, n) {if(x === 1 || n === 0){return 1}if(x===-1){return n % 2 ? -1 : 1}let res = 1if(n>0){for(let i = 0;i<n;i++){res *= x}return res}for(let i = 0;i<-n;i++){res /= xif(x>0 && res<0.000005){return res}}return res
}
解题思路-分治
上面的解题思路虽然能解题,但是因为要真实的进行每一次计算,所以效率比较低。那如何才能提高效率呢?这里我们可以采用类似二分的方法,将 x 的 n 次方拆分为 x^(n/2) * x^(n/2),以此来加速计算的过程。每次拆分一半,直到 n = 0。因为每次的处理逻辑是相同的,所以可以利用递归函数递归调用自己,而退出条件就是 n = 0。
代码实现
var myPow = function(x, n) {if(n == 0){return 1}if(n < 0){return 1 / myPow(x, -n)}if(n % 2){return x * myPow(x, n - 1)}return myPow(x * x, n / 2)
}
至此我们就完成了 leetcode-50-Pow(x, n)
如有任何问题或建议,欢迎留言讨论!
边栏推荐
- 理财产品锁定期是什么意思?理财产品在锁定期能赎回吗?
- 别用Xshell了,试试这个更现代的终端连接工具
- Thinkphp历史漏洞复现
- 限流对比:Sentinel vs Hystrix 到底怎么选?
- 第一章概述-------第一节--1.3互联网的组成
- “青出于蓝胜于蓝”,为何藏宝计划(TPC)是持币生息最后的一朵白莲花
- FIR filter design
- The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
- 如何保证缓存和数据库一致性
- 带你一分钟了解对称加密和非对称加密
猜你喜欢

“青出于蓝胜于蓝”,为何藏宝计划(TPC)是持币生息最后的一朵白莲花

How to ensure cache and database consistency

Win11如何关闭共享文件夹

Idea Alibaba cloud multi module deployment

"Green is better than blue". Why is TPC the last white lotus to earn interest with money

Merge multiple row headers based on apache.poi operation

Take you a minute to learn about symmetric encryption and asymmetric encryption

Pyqt5 rapid development and practice 3.4 signal and slot correlation
![[express receives get, post, and route request parameters]](/img/6c/ac936a8dff50b803993bef5192723b.png)
[express receives get, post, and route request parameters]

【开发教程9】疯壳·ARM功能手机-I2C教程
随机推荐
The first case in Guangdong! A company in Guangzhou was punished by the police for failing to fulfill its obligation of data security protection
Threads and processes
My SQL is OK. Why is it still so slow? MySQL locking rules
操作系统迁移实战之在openEuler上部署MySQL数据库
Thoroughly uncover how epoll realizes IO multiplexing
Review the past and know the new MySQL isolation level
[express receives get, post, and route request parameters]
Win11自动删除文件设置方法
Execution process of select statement in MySQL
mysql锁机制(举例说明)
PyQt5快速开发与实战 3.2 布局管理入门 and 3.3 Qt Designer实战应用
【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸
Who is safe to open the VIP account of CICC securities?
Method and voltage setting of exciting vibrating wire sensor with hand-held vibrating wire collector
Sharing of 40 completed projects of high-quality information management specialty [source code + Thesis] (VI)
2022软件测试技能 Postman+newman+jenkins 持续集成 实战教程
Batch normalization batch_ normalization
How does the data link layer transmit data
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
【无标题】