当前位置:网站首页>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
.
边栏推荐
- What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
- Learning of mall permission module
- Interview questions for famous enterprises: Coins represent a given value
- 如何向mongoDB中添加新的字段附代码(全)
- Hysbz 2243 staining (tree chain splitting)
- Blocking of concurrency control
- The difference between MVVM and MVC
- 微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
- Two stage locking protocol for concurrency control
- Web3为互联网带来了哪些改变?
猜你喜欢

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

Database tuning solution

How can Bluetooth in notebook computer be used to connect headphones

The difference between MVVM and MVC

Interprocess communication in the "Chris Richardson microservice series" microservice architecture

database mirroring

Official clarification statement of Jihu company

Solutions for unexplained downtime of MySQL services

Granularity of blocking of concurrency control

Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
随机推荐
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
Official clarification statement of Jihu company
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
Poj3414 extensive search
Performance testing of software testing
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Oracle triggers
如何开发引入小程序插件
Matlab draws a cute fat doll
How to add new fields to mongodb with code (all)
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Kubernetes Administrator certification (CKA) exam notes (IV)
Promql demo service
Business learning of mall commodity module
Meituan dynamic thread pool practice ideas, open source
Business learning of mall order module
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
C language knowledge points link
boundary IoU 的计算方式
Learning of mall permission module