当前位置:网站首页>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;
}
}
边栏推荐
- 记一次 .NET 某物管后台服务 卡死分析
- 怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
- Custom multithreading base class threading Event
- i. Construction of mx6ull C language environment
- mybaitis生成器详解
- Deep understanding of happens before principle
- MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
- Minimum editing distance (linear DP writing method)
- Dynamic programming [III] (interval DP) stone merging
- Maximum path and problem (cherry picking problem)
猜你喜欢

秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商

Jwas: a Bayesian based GWAS and GS software developed by Julia

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)

Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022

MapReduce principle analysis (in-depth source code)

Wechat applet realizes five-star evaluation

mysql学习1:安装mysql

MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)

In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
Interview shock 60: what will cause MySQL index invalidation?
随机推荐
居家办公被催之后才明白的时间管理
号称史上最难618,淘宝数据盘点你做对了吗?
对象序列化
ssh工作流程及原理
Interview shock 60: what will cause MySQL index invalidation?
application. Configuration information of properties
log4j.properties的配置详解
57. The core principle of flutter - layout process
深入理解 happens-before 原则
JMeter connection DM8
Interview shock 60: what will cause MySQL index invalidation?
uniapp下拉弹层选择框效果demo(整理)
Neo4j:入门基础(一)之安装与使用
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
Interviewer: with the for loop, why do you need foreach?
解开C语言的秘密《关键字》(第六期)
MySQL high level statements (I)
How histrix works
手把手带你入门 API 开发
$15.8 billion! 2021 the world's top15 most profitable hedge fund giant