当前位置:网站首页>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
.
边栏推荐
- 2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
- Installation of VMware Workstation
- 微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
- How to view Apache log4j 2 remote code execution vulnerability?
- FBO and RBO disappeared in webgpu
- Oracle views the data size of a table
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- Matlab draws a cute fat doll
- Multiplexing of Oracle control files
- Overview of concurrency control
猜你喜欢
Technology cloud report: how many hurdles does the computing power network need to cross?
Alternating merging strings of leetcode simple questions
Win11运行cmd提示“请求的操作需要提升”的解决方法
Business learning of mall commodity module
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Interview questions for famous enterprises: Coins represent a given value
How to view Apache log4j 2 remote code execution vulnerability?
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
Sparse array [matrix]
随机推荐
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Blocking protocol for concurrency control
IIC bus realizes client device
Recovery technology with checkpoints
A trip to Suzhou during the Dragon Boat Festival holiday
EBS Oracle 11g cloning steps (single node)
A substring with a length of three and different characters in the leetcode simple question
Performance monitoring of database tuning solutions
C language knowledge points link
装饰器学习01
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Leetcode simple question: find the nearest point with the same X or Y coordinate
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
Solutions for unexplained downtime of MySQL services
Stored procedures and stored functions
Common interview questions of redis factory
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
How to organize an actual attack and defense drill
[Yugong series] go teaching course in July 2022 004 go code Notes
微服务链路风险分析