当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

行业洞察 | 新零售业态下,品牌电商应如何重塑增长?

Nifi from introduction to practice (nanny level tutorial) - identity authentication

pull request

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

Usage of rxjs mergemap

Picocli getting started

DM8:达梦数据库-锁超时

uniapp下拉弹层选择框效果demo(整理)

MySQL learning 1: installing MySQL

Uniapp drop-down layer selection box effect demo (sorting)
随机推荐
最短编辑距离(线性dp写法)
Interview shock 60: what will cause MySQL index invalidation?
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
JMeter connection DM8
nifi从入门到实战(保姆级教程)——身份认证
Comment modifier Node Fichiers dans les modules
MapReduce原理剖析(深入源码)
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Topic38——56. Consolidation interval
建木持续集成平台v2.5.0发布
Minimum editing distance (linear DP writing method)
Topic37——64. Minimum path sum
alibaba jarslink
Mit6.031 software construction7 reading notesdesigning specifications
word文本框换页
Four memory areas (stack, heap, global, code area)
Master formula
uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
和动态规划的第一次相遇
Online bidding of Oracle project management system