当前位置:网站首页>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
.
边栏推荐
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- 笔记本电脑蓝牙怎么用来连接耳机
- Metaverse Ape获Negentropy Capital种子轮融资350万美元
- Index optimization of performance tuning methodology
- 等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
- What about data leakage? " Watson k'7 moves to eliminate security threats
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- Overview of concurrency control
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- 微服务链路风险分析
猜你喜欢

Overview of concurrency control
![Sparse array [matrix]](/img/62/27b02deeeaa5028a16219ef51ccf82.jpg)
Sparse array [matrix]

极狐公司官方澄清声明

ICMP introduction

MySQL服务莫名宕机的解决方案

Performance monitoring of database tuning solutions

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

The simple problem of leetcode is to split a string into several groups of length K

Web3为互联网带来了哪些改变?

Pl/sql basic syntax
随机推荐
如何快速体验OneOS
每日刷题记录 (十四)
Storage optimization of performance tuning methodology
Sentinel production environment practice (I)
Alternating merging strings of leetcode simple questions
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Platform bus
Comment développer un plug - in d'applet
笔记本电脑蓝牙怎么用来连接耳机
Livelocks and deadlocks of concurrency control
database mirroring
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Draw a red lantern with MATLAB
Character conversion PTA
Matlab draws a cute fat doll
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
HDU 4391 paint the wall segment tree (water
Poj3414 extensive search
Some tutorials install the database on ubantu so as not to occupy computer memory?
Calculation method of boundary IOU