当前位置:网站首页>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 .
边栏推荐
- IIC bus realizes client device
- Dbeaver executes multiple insert into error processing at the same time
- Serializability of concurrent scheduling
- How to use tensorflow2 for cat and dog classification and recognition
- HDU 4391 paint the wall segment tree (water
- [agc009e] eternal average - conclusion, DP
- Oracle is sorted by creation time. If the creation time is empty, the record is placed last
- Common interview questions of JVM manufacturers
- 科技云报道:算力网络,还需跨越几道坎?
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
猜你喜欢
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Common interview questions of redis factory
AD637使用筆記
Practice: fabric user certificate revocation operation process
Technology cloud report: how many hurdles does the computing power network need to cross?
Pl/sql basic syntax
K210学习笔记(四) K210同时运行多个模型
The real situation of programmers
Learning of mall permission module
Three "factions" in the metauniverse
随机推荐
Text组件新增内容通过tag_config设置前景色、背景色
Talking about MySQL index
Microservice link risk analysis
CRM creates its own custom report based on fetch
FBO and RBO disappeared in webgpu
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
What changes has Web3 brought to the Internet?
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Platformio create libopencm3 + FreeRTOS project
CA certificate trampled pit
About the writing method of SQL field "this includes" and "included in" strings
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
元宇宙中的三大“派系”
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Recovery technology with checkpoints
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
854. String BFS with similarity K
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
Blocking protocol for concurrency control
Performance testing of software testing