当前位置:网站首页>LeetCode_ Fast power_ Recursion_ Medium_ 50.Pow(x, n)
LeetCode_ Fast power_ Recursion_ Medium_ 50.Pow(x, n)
2022-06-27 12:28:00 【I've been up and down in the Jianghu】
1. subject
Realization pow(x, n) , Computation x The integer of n Power function ( namely ,xn ).
Example 1:
Input :x = 2.00000, n = 10
Output :1024.00000
Example 2:
Input :x = 2.10000, n = 3
Output :9.26100
Example 3:
Input :x = 2.00000, n = -2
Output :0.25000
explain :2-2 = 1/22 = 1/4 = 0.25
Tips :
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104
source : Power button (LeetCode)
link :https://leetcode.cn/problems/powx-n
2. Ideas
(1) Fast power _ recursive
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— Fast power _ recursive
class Solution {
public double myPow(double x, int n) {
long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
private double quickMul(double x, long N) {
if (N == 0) {
return 1.0;
}
double y = quickMul(x, N / 2);
return N % 2 == 0 ? y * y : y * y * x;
}
}
边栏推荐
猜你喜欢

Topic37——64. 最小路径和

xxl-job学习梳理

$15.8 billion! 2021 the world's top15 most profitable hedge fund giant

56. Core principle of flutter - flutter startup process and rendering pipeline

Histrix工作原理

MapReduce实战小案例(自定义排序、二次排序、分组、分区)

Tidb 6.0: making Tso more efficient tidb Book rush

Browser cookie to selenium cookie login
![[on Nacos] get started quickly](/img/cc/af4ab640952b880595a89f66688ff5.jpg)
[on Nacos] get started quickly

alibaba jarslink
随机推荐
Interview shock 60: what will cause MySQL index invalidation?
Maximum path and problem (cherry picking problem)
面试突击60:什么情况会导致 MySQL 索引失效?
On ticheck
消息隊列的使用
In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
LeetCode_快速幂_递归_中等_50.Pow(x, n)
解压 .log.gz 文件
56. Core principle of flutter - flutter startup process and rendering pipeline
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
master公式
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Utilisation de la file d'attente des messages
全球最强截图软件 Snipaste
Hands on API development
Dynamic programming [III] (interval DP) stone merging
pull request
记一次 .NET 某物管后台服务 卡死分析
微服务拆分
Fork/Join 框架基本使用和原理