当前位置:网站首页>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;
}
}
边栏推荐
- 面试突击60:什么情况会导致 MySQL 索引失效?
- Use of message queues
- [tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)
- 全志A13折腾备忘
- pull request
- 深入理解 happens-before 原则
- This privatized deployed enterprise knowledge base makes telecommuting a zero distance
- 全球最强截图软件 Snipaste
- Four memory areas (stack, heap, global, code area)
- MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
猜你喜欢

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

MySQL high level statements (I)

一个有趣的网络掩码的实验

This privatized deployed enterprise knowledge base makes telecommuting a zero distance
![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)
Dynamic programming [4] (counting class DP) example: integer partition

MapReduce principle analysis (in-depth source code)

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

pull request

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

私藏干货分享:关于企业架构中如何进行平台化
随机推荐
Interviewer: with the for loop, why do you need foreach?
. Net6 access skywalking link tracking complete process
自定义多线程基类threading.Event
如何修改 node_modules 裏的文件
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
Interview shock 60: what will cause MySQL index invalidation?
DM8:达梦数据库-锁超时
What is the TCP 3-time handshake process?
Custom multithreading base class threading Event
alibaba jarslink
word文本框换页
C # WPF realizes undo redo function
消息隊列的使用
Raspberry pie 3b+ learning
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Self taught ADT and OOP
Wechat applet payment password input
对象序列化
Microservice splitting
解除百度文库VIP、语雀、知乎付费限制,原来这么简单