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

In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
![Dynamic programming [III] (interval DP) stone merging](/img/a4/82c4d63b8df5d092a96b80dd497147.jpg)
Dynamic programming [III] (interval DP) stone merging

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

剑指 Offer 04. 二维数组中的查找

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

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

On ticheck

Histrix工作原理

How to modify a node_ Files in modules

Sword finger offer 04 Find in 2D array
随机推荐
Dynamic programming [4] (counting class DP) example: integer partition
A brief talk on cordola tree
57. The core principle of flutter - layout process
AI for Science:科研范式、开源平台和产业形态
Topic37——64. Minimum path sum
Dm8: Dameng database - lock timeout
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
浏览器cookie转selenium cookie登录
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
Getting started with go web programming: validators
master公式
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
MapReduce原理剖析(深入源码)
关于枚举类的两种用法
Mybaitis generator details
log4j.properties的配置详解
Two usages of enumeration classes
PyQt,PySide-槽函数被执行了两次
Tidb 6.0: making Tso more efficient tidb Book rush
Wechat applet realizes five-star evaluation