当前位置:网站首页>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
.
边栏推荐
- The real situation of programmers
- Multiplexing of Oracle control files
- How to view Apache log4j 2 remote code execution vulnerability?
- Leetcode simple question: check whether each row and column contain all integers
- 如何快速体验OneOS
- Performance monitoring of database tuning solutions
- Ad637 notes d'utilisation
- Kubernetes Administrator certification (CKA) exam notes (IV)
- Bitbucket installation configuration
- MySQL服务莫名宕机的解决方案
猜你喜欢

Oracle hint understanding

Shell script, awk condition judgment and logic comparison &||

boundary IoU 的计算方式

微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)

Meituan dynamic thread pool practice ideas, open source

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

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

每日刷题记录 (十四)

Three "factions" in the metauniverse

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
随机推荐
Two stage locking protocol for concurrency control
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Database tuning solution
What changes has Web3 brought to the Internet?
Performance testing of software testing
DataGrid directly edits and saves "design defects"
Character conversion PTA
Draw a red lantern with MATLAB
如何開發引入小程序插件
Learning of mall permission module
Platform bus
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Bitbucket installation configuration
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
How to add new fields to mongodb with code (all)
About the writing method of SQL field "this includes" and "included in" strings
Shell script, awk uses if, for process control
微服务链路风险分析