当前位置:网站首页>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 .
边栏推荐
- Meituan dynamic thread pool practice ideas, open source
- AD637使用笔记
- [Yugong series] go teaching course in July 2022 004 go code Notes
- DataGrid directly edits and saves "design defects"
- Shell script, awk condition judgment and logic comparison &||
- Concurrency control of performance tuning methodology
- Server optimization of performance tuning methodology
- database mirroring
- Character conversion PTA
- Win11运行cmd提示“请求的操作需要提升”的解决方法
猜你喜欢
A trip to Suzhou during the Dragon Boat Festival holiday
Decorator learning 01
Depth first DFS and breadth first BFS -- traversing adjacency tables
Shell script, awk condition judgment and logic comparison &||
Pl/sql basic syntax
[Yugong series] go teaching course in July 2022 004 go code Notes
database mirroring
Leetcode simple question ring and rod
Leetcode simple question: check whether each row and column contain all integers
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
随机推荐
How to develop and introduce applet plug-ins
Character conversion PTA
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
PyGame practical project: write Snake games with 300 lines of code
Win11运行cmd提示“请求的操作需要提升”的解决方法
Solutions for unexplained downtime of MySQL services
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
Text组件新增内容通过tag_config设置前景色、背景色
Business learning of mall order module
Some tutorials install the database on ubantu so as not to occupy computer memory?
Damn, window in ie open()
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Leetcode simple question: check whether each row and column contain all integers
Kubernetes Administrator certification (CKA) exam notes (IV)
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
K210 learning notes (IV) k210 runs multiple models at the same time
Leetcode simple question: find the nearest point with the same X or Y coordinate
科技云报道:算力网络,还需跨越几道坎?
DataGrid directly edits and saves "design defects"
Three "factions" in the metauniverse