当前位置:网站首页>50. Pow(x, n). O(logN) Sol
50. Pow(x, n). O(logN) Sol
2022-07-05 22:17:00 【isee_ nh】
Move the dichotomy of the official solution , The time complexity is O(log)
Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
class Solution {
public:
double fastPow(double x, long long n) {
if (n == 0) {
return 1.0;
}
double half = fastPow(x, n / 2);
if (n % 2 == 0) {
return half * half;
} else {
return half * half * x;
}
}
double myPow(double x, int n) {
long long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
return fastPow(x, N);
}
};
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 Explanation: 2-2 = 1/22 = 1/4 = 0.25
Constraints:
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104
Approach 2: Fast Power Algorithm Recursive
Intuition
Assuming we have got the result of , how can we get
? Obviously we do not need to multiply
for another
times. Using the formula
, we can get
at the cost of only one computation. Using this optimization, we can reduce the time complexity of our algorithm.
Algorithm
Assume we have got the result of , and now we want to get the result of
. Let
A
be result of , we can talk about
based on the parity of
n
respectively. If n
is even, we can use the formula to get
. If
n
is odd, then . Intuitively, We need to multiply another xx to the result, so
. This approach can be easily implemented using recursion. We call this method "Fast Power", because we only need at most O(logn) computations to get
.
边栏推荐
- Sparse array [matrix]
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
- What about data leakage? " Watson k'7 moves to eliminate security threats
- How to view Apache log4j 2 remote code execution vulnerability?
- 如何向mongoDB中添加新的字段附代码(全)
- Oracle views the data size of a table
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Installation of VMware Workstation
- 微服务链路风险分析
- 装饰器学习01
猜你喜欢
Overview of concurrency control
Official clarification statement of Jihu company
Practice: fabric user certificate revocation operation process
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
极狐公司官方澄清声明
K210学习笔记(四) K210同时运行多个模型
The American Championship is about to start. Are you ready?
Concurrency control of performance tuning methodology
Ad637 notes d'utilisation
随机推荐
Hcip day 16
boundary IoU 的计算方式
Shell script, awk condition judgment and logic comparison &||
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
如何快速体验OneOS
Database recovery strategy
Web3为互联网带来了哪些改变?
Matlab draws a cute fat doll
Summary of El and JSTL precautions
database mirroring
MySQL服务莫名宕机的解决方案
"Chris Richardson microservices series" uses API gateway to build microservices
HDU 4391 paint the wall segment tree (water
[Yugong series] go teaching course in July 2022 004 go code Notes
How to use tensorflow2 for cat and dog classification and recognition
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
The American Championship is about to start. Are you ready?
Metaverse Ape上线倒计时,推荐活动火爆进行
Character conversion PTA
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖