class Solution {
public double myPow(double x, int n) {
double res = 1;
long y = n;
// 如果 n 为负数,将 x 变为其倒数,n 变为正数
if (n < 0) {
y = -y;
x = 1 / x;
}
// 快速幂算法,将 x 的 n 次方分解为若干个平方的乘积
while (y > 0) {
// 如果当前 n 为奇数,将结果乘以 x
if (y % 2 1) {
res *= x;
}
// 将 x 变为其平方
x *= x;
// 将 n 减半
y /= 2;
}
// 返回计算结果
return res;
}
}
class Solution {
public double myPow(double x, int n) {
if (x == 0) {
return 0;
}
double res = 1;
long b = n;
if (b < 0) {
x = 1 / x;
b = -b;
}
while (b > 0) {
/// 奇数
if ((b & 1) == 1) {
res *= x;
}
x *= x;
b = b >> 1;
}
return res;
}
}
class Solution {
public:
double myPow(double x, int m) {
double res = 1.0;
long n = m;
if(n < 0)
{
x = 1/x;
n = -n;
}
while(n){
if(n % 2) res *= x;
x *= x;
n >>= 1;
}
return res;
}
};
时间复杂度:O(log2N)空间复杂度O(1)
class Solution {
public:
double myPow(double x, int n) {
double result = 1;
int y = n;
if(n<0){
y = -n;
x = 1/x;
}
while(y != 0){
if(y%2 == 1)result *= x;
x = x*x;
y /= 2;
}
return result;
}
};
class Solution {
public:
double myPow(double x, int n) {
double res = 1;
// 试了下,使用uint也行
long y = n;
if( n < 0){
y = -y;
x = 1 / x;
}
while(y > 0){
// 判断 y最有一位是否为1
if(y & 1 == 1){
res = res * x;
}
x *= x;
// y右移1位
y = y >> 1;
}
return res;
}
};
class Solution {
public:
double myPow(double x, int n) {
long long N = n;
if(n < 0) N = -N;
double res = 1.0;
while(N) {
if(N & 1) res *= x;
x *= x;
N >>= 1;
}
return n < 0 ? 1 / res : res;
}
};
class Solution:
def myPow(self, x: float, n: int) -> float:
if x 0.0: return 0.0
res = 1
if n < 0: x, n = 1 / x, -n
while n:
if n & 1: res *= x
x *= x
n >>= 1
return res
log n O 1 时间
class Solution {
public double myPow(double x, int n) {
double res = 1;
long y = n; //如果是int的话可能会溢出
if(n < 0){ //如果n<0,就转一下做
y = -y;
x = 1 / x;
}
while(y > 0){
if(y % 2 == 1){ //说明最后一位是1,需要让res乘以这一位的权重
res = res * x;
}
x = x * x;
y = y / 2; //丢掉最后一位
}
return res;
}
}
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function (x, n) {
let res = 1
let y = x, m = n
if (n < 0) { //若幂次小于0,变为正数
m = -n
y = 1 / x
}
while (m > 0) { //当幂次大于0
if (m % 2 === 1) { //如果是奇数,
res = res * y
}
y = y * y
m = Math.floor(m / 2)
}
return res
};
class Solution {
public double myPow(double x, int n) {
double result = 1;
long y = n;//如果是int的话可能会溢出
if(n<0){
y = -y;
x = 1/x;
}
while(y>0){
if((y&1)==1){//说明最后一位是1,需要让result乘以这一位的权重
result = result*x;
}
y = y >> 1;
x = x*x;
}
return result;
}
}
思路:m的n次方,其中n可以改为2的幂的和,比如3^(8+4+1)=3^8*3^4*3^1,也就是说让每一个权重进行相乘就行,而权重可以对n(13)进行向右移动获取。
“`c++
class Solution {
public:
double myPow(double x, int n) {
long long N = n;
if (n < 0) {
N = -N;
}
double temp = x;
double ans = 1;
while (N != 0) {
if (N & 1 == 1) {
ans = ans * temp;
}
temp *= temp;
N >>= 1;
}
return n < 0 ? 1/ans : ans;
}
};
```
共以下 18 个回答
class Solution {
public double myPow(double x, int a) {
double ans = 1;
long n = a;
n = Math.abs(n);
}
long型
class Solution {
public double myPow(double x, int n) {
double res = 1;
long y = n;
// 如果 n 为负数,将 x 变为其倒数,n 变为正数
if (n < 0) {
y = -y;
x = 1 / x;
}
// 快速幂算法,将 x 的 n 次方分解为若干个平方的乘积
while (y > 0) {
// 如果当前 n 为奇数,将结果乘以 x
if (y % 2 1) {
res *= x;
}
// 将 x 变为其平方
x *= x;
// 将 n 减半
y /= 2;
}
// 返回计算结果
return res;
}
}
时间复杂度 O(logn) 二分的时间复杂度为对数级别。
空间复杂度 O(1) 常数大小额外空间。
时间复杂度:O(logn)
空间复杂度:O(1)
public double myPow(double x, int n) {
if(x0.0f) return 0.0d;
double res=1.0;
long b=n;
if(b<0){
x=1/x;
b=-b;
}
while(b>0){
if((b&1)1) res=x;
x=x;
b>>=1;
}
return res;
}
思路:数学方法
class Solution {
public:
double myPow(double x, int n) {
};
class Solution {
public:
double myPow(double x, int m) {
double res = 1.0;
long n = m;
if(n < 0)
{
x = 1/x;
n = -n;
}
while(n){
if(n % 2) res *= x;
x *= x;
n >>= 1;
}
return res;
}
};
时间复杂度:O(log2N)空间复杂度O(1)
分治方法,时间复杂度O(logn),空间复杂度O(1)
时间复杂度O(logn),空间复杂度O(1)
打卡
class Solution:
def myPow(self, x: float, n: int) -> float:
if x 0.0: return 0.0
res = 1
if n < 0: x, n = 1 / x, -n
while n:
if n & 1: res *= x
x *= x
n >>= 1
return res
log n O 1 时间
class Solution {
public double myPow(double x, int n) {
if(x0.0f) return 0;
long b = n;
if(b < 0){
x = 1 / x;
b = – b;
}
double ans = 1;
double temp = x;
while(b!=0){
if((b & 1) 1){
ans *= temp;
}
temp *= temp;
b = b >> 1;
}
return ans;
}
}
//时间复杂度:O(logn)
//空间复杂度:O(1)
时间复杂度:O(logn)
空间复杂度:O(1)
day16第二题
思路:参考位运算
“`c++
class Solution {
public:
double myPow(double x, int n) {
long long N = n;
if (n < 0) { N = -N; } double temp = x; double ans = 1; while (N != 0) { if (N & 1 == 1) { ans = ans * temp; } temp *= temp; N >>= 1;
}
return n < 0 ? 1/ans : ans; } }; ```