当前位置:网站首页>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
.
边栏推荐
- Pinctrl subsystem and GPIO subsystem
- MySQL actual combat 45 lecture learning (I)
- Learning of mall permission module
- Dbeaver executes multiple insert into error processing at the same time
- Alternating merging strings of leetcode simple questions
- A trip to Suzhou during the Dragon Boat Festival holiday
- 元宇宙中的三大“派系”
- MySQL服务莫名宕机的解决方案
- Unique occurrence times of leetcode simple questions
- Summary of El and JSTL precautions
猜你喜欢

AD637使用筆記

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/9d/7d01bc1daa61f6545f619b6746f8bb.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022

How can Bluetooth in notebook computer be used to connect headphones

A substring with a length of three and different characters in the leetcode simple question

Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award

科技云报道:算力网络,还需跨越几道坎?

每日刷题记录 (十四)

AD637 usage notes

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
随机推荐
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Serializability of concurrent scheduling
Livelocks and deadlocks of concurrency control
Two stage locking protocol for concurrency control
Summary of El and JSTL precautions
科技云报道:算力网络,还需跨越几道坎?
Regular expressions and re Libraries
What changes has Web3 brought to the Internet?
AD637 usage notes
Granularity of blocking of concurrency control
Three "factions" in the metauniverse
Unique occurrence times of leetcode simple questions
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
K210学习笔记(四) K210同时运行多个模型
HDU 4391 paint the wall segment tree (water
C language knowledge points link
The American Championship is about to start. Are you ready?