当前位置:网站首页>LeetCode_快速幂_递归_中等_50.Pow(x, n)
LeetCode_快速幂_递归_中等_50.Pow(x, n)
2022-06-27 12:03:00 【一瓢江湖我沉浮】
1.题目
实现 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/powx-n
2.思路
(1)快速幂_递归
思路参考本题官方题解。
3.代码实现(Java)
//思路1————快速幂_递归
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;
}
}
边栏推荐
- Nifi from introduction to practice (nanny level tutorial) - identity authentication
- 消息隊列的使用
- 面试突击60:什么情况会导致 MySQL 索引失效?
- threejs的环境光+点光源+平行光源+球面光 以及hepler理解+阴影()
- log4j.properties的配置详解
- R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
- How to adjust an integer that is entered in Excel but always displays decimals?
- In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
- 解开C语言的秘密《关键字》(第六期)
- R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
猜你喜欢

Usage of rxjs mergemap

Neo4j:入门基础(一)之安装与使用

MySQL learning 1: installing MySQL

面试突击60:什么情况会导致 MySQL 索引失效?

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

MySQL high level statements (I)

Drive to APasS! Use Mingdao cloud to manage F1 events

Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)

How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line

AI for Science:科研范式、开源平台和产业形态
随机推荐
自学ADT和OOP
关于枚举类的两种用法
$15.8 billion! 2021 the world's top15 most profitable hedge fund giant
alibaba jarslink
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
What is the TCP 3-time handshake process?
Talk about go language and cloud native technology
alibaba jarslink
Basic usage and principle of fork/join framework
nifi从入门到实战(保姆级教程)——身份认证
浏览器cookie转selenium cookie登录
uniapp下拉弹层选择框效果demo(整理)
亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?
Hibernate operation Oracle database primary key auto increment
手把手带你入门 API 开发
Write it down once Net analysis of a property management background service stuck
建木持续集成平台v2.5.0发布
Histrix工作原理
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
一个有趣的网络掩码的实验