当前位置:网站首页>leetcode:50. Pow(x, n)
leetcode:50. Pow(x, n)
2022-07-17 00:51:00 【uncle_ll】
50. Pow(x, n)
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/powx-n/
实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即, x n x^n xn )。
示例 1:
输入:x = 2.00000, n = 10
输出:1024.00000
示例 2:
输入:x = 2.10000, n = 3
输出:9.26100
示例 3:
输入:x = 2.00000, n = -2
输出:0.25000
解释:2^-2 = 1/2^2 = 1/4 = 0.25
提示:
- -100.0 < x < 100.0
- − 2 31 -2^{31} −231 <= n <= 2 3 1 2^31 231-1
- − 1 0 4 -10^4 −104 <= x n x^n xn <= 1 0 4 10^4 104
解法
暴力法: for循环,n次相乘,时间复杂度O(N)
递归:分治法,分奇偶
- 分治法递归
- 指数二进制分治法循环


注意x与n的边界,x为0,n正负问题,这里 0 0 0^0 00约定于0
代码实现
递归
python实现
class Solution:
def myPow(self, x: float, n: int) -> float:
if x == 0:
return 0
if n == 0:
return 1
if n < 0:
n = -n
x = 1 / x
def helper(x: float, n: int) -> float:
if n == 0:
return 1
sub_res = helper(x, n//2)
return x * sub_res * sub_res if n & 1 else sub_res * sub_res
return helper(x, n)
c++实现
class Solution {
public:
double pow(double x, long long n) {
if (n == 0) return 1;
double sub_res = pow(x, n/2);
return n&1 ? x * sub_res * sub_res : sub_res * sub_res;
}
double myPow(double x, int n) {
if (x == 0) return 0;
if (n == 0) return 1;
if (n < 0) {
long long n = -1 * n;
x = 1 / x;
}
return pow(x, n);
}
};
复杂度分析
- 时间复杂度: O ( l o g N ) O(logN) O(logN)
- 空间复杂度: O ( l o g N ) O(logN) O(logN)
二进制分治法迭代
python实现
class Solution:
def myPow(self, x: float, n: int) -> float:
if x == 0:
return 0
if n == 0:
return 1
if n < 0:
n = -n
x = 1 / x
res = 1.0
while n:
if n & 1:
res *= x
x *= x
n >>= 1
return res
c++实现
class Solution {
public:
double myPow(double x, int n) {
if (x == 0) return 0;
if (n == 0) return 1;
long int N = n;
if (n < 1) {
N = -N;
x = 1/x;
}
double res = 1.0;
while (N) {
if (N & 1)
res *= x;
x *= x;
N >>= 1;
}
return res;
}
};
复杂度分析
- 时间复杂度: O ( l o g N ) O(logN) O(logN)
- 空间复杂度: O ( 1 ) O(1) O(1)
参考
边栏推荐
- Mysql多表查询
- A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
- 【单片机仿真】(六)寻址方式 — 变址寻址与相对寻址
- Can't access this website can't find DNS address DNS_ PROBE_ What about started?
- Use RZ, SZ commands to upload and download files through xshell7
- [template record] string hash to judge palindrome string
- 多表查询——案例练习
- SQL经典练习题(x30)
- Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
- Yolov6 learning first chapter
猜你喜欢

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
![In depth understanding of machine learning - unbalanced learning: sample sampling technology - [smote sampling method and borderline smote sampling method of manual sampling technology]](/img/9f/a0d03b23e66849f12150f9a72f36c5.png)
In depth understanding of machine learning - unbalanced learning: sample sampling technology - [smote sampling method and borderline smote sampling method of manual sampling technology]

The WinRAR command copies the specified folder as a compressed file, and calls the scheduled task for backup.

无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?
![[MCU simulation] (XX) org - set start address](/img/9e/4e44dd779b0de28a190d86fbb1c2c0.png)
[MCU simulation] (XX) org - set start address

05_服务调用Ribbon

Bisenetv2 face segmentation

Comparison between redis and other databases

MySQL optimized index

MySQL interview questions (2022)
随机推荐
Is there really no way out for functional testing? 10K capping is never a joke
ES6 learning notes - brother Ma at station B
zsh: command not found: mysql
MySQL multi table query
C语言基础Day4-数组
My most productive easypypi once again has been updated! V1.4.0 release
Unicast、Multicast、Broadcast
内置键盘连续444
[MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
关于XML文件(六)-与JSON的区别
Rhce8 Learning Guide Chapter 1 installing rhel8.4
我最高产的EasyPyPI又双叒叕更新了!v1.4.0发布
SQL classic exercises (x30)
Yolov5 ncnn reasoning
A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
zsh: command not found: mysql
[NoSQL] redis configuration and optimization of NoSQL (simple operation)
Obvious things
[single chip microcomputer simulation] (10) instruction system - multiplication instruction and division of arithmetic operation instruction