当前位置:网站首页>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
.
边栏推荐
- A trip to Suzhou during the Dragon Boat Festival holiday
- 装饰器学习01
- The real situation of programmers
- Concurrency control of performance tuning methodology
- Type of fault
- PyGame practical project: write Snake games with 300 lines of code
- The simple problem of leetcode is to split a string into several groups of length K
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- How to add new fields to mongodb with code (all)
- Leetcode simple question ring and rod
猜你喜欢

How to view Apache log4j 2 remote code execution vulnerability?

Talking about MySQL index

Oracle triggers

Practice: fabric user certificate revocation operation process

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

每日刷题记录 (十四)

数据泄露怎么办?'华生·K'7招消灭安全威胁

Overview of database recovery

笔记本电脑蓝牙怎么用来连接耳机

Summary of concurrency control
随机推荐
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
Performance monitoring of database tuning solutions
Pinctrl subsystem and GPIO subsystem
Concurrency control of performance tuning methodology
Character conversion PTA
如何开发引入小程序插件
Promql demo service
Summary of El and JSTL precautions
Microservice link risk analysis
Leetcode simple question: the minimum cost of buying candy at a discount
Web3为互联网带来了哪些改变?
Recovery technology with checkpoints
装饰器学习01
Metaverse Ape上线倒计时,推荐活动火爆进行
Nacos 的安装与服务的注册
Win11运行cmd提示“请求的操作需要提升”的解决方法
Type of fault
ESP32 hosted
Oracle advanced query
PyGame practical project: write Snake games with 300 lines of code