class Solution {
public:
double myPow(double x, int n) {
double res = 1.0;
long y = n;
if(n < 0){
y = -y;
x = 1 / x;
}
while(y){
if(y & 1) res *= x;
x *= x;
y >>= 1;
}
return res;
}
};
c++ 位运算真的强
class Solution {
public:
double myPow(double x, int n) {
if (n 0) return 1;
if (n 1) return x;
if (n -1) return 1 / x;
double h = myPow(x, n >> 1);
double m = myPow(x, n & 1);
return h * h * m;
}
};
”’java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ret = 0;
while(n != 0){
n &= n-1;//将二进制位中最低位的1变为0之后的结果
ret++;
class Solution {
public double myPow(double x, int n) {
double res = 1;
long y = n;//不能用int
if(n < 0){
y = -y;
x = 1 / x;
}
while(y > 0){
if(y % 2 == 1){
res *= x;
}
x = x * x;
y = y / 2;
}
return res;
}
}
class Solution {
public double myPow(double x, int n) {
double res = 1;
long y = n;
if(n<0){
y = -y;
x = 1/x;
}
while(y>0){
if(y%2==1){
res = res * x;
}
x = x*x;
y = y/2;
}
return res;
}
}
func myPow(x float64, n int) float64 {
res := 1.0
y := int64(n)
//负数
if n < 0 {
y = -y
//倒数
x = 1 / x
}
for y > 0 {
if y % 2 == 1 {
res *= x
}
x *= x
y /= 2
}
return res
}
class Solution {
public double myPow(double x, int n) {
// 假设 2 的 -2 次方
double res = 1.0;
long b = n;
if (b < 0) {
x = 1/x;
b = -b;
}
while (b > 0) {
if ((b&1) == 1) {
res = res * x;
}
// 每移动一位就成个x,相当于把1位置的权重*1 x是标记权重的 示例2的13次方 3的13次方
x = x * x;
b = b >> 1;
}
return res;
}
}
function myPow(x: number, n: number): number {
if (x === 0) return 0;
if (n < 0) {
x = 1 / x;
n = -1 * n;
}
let res = 1;
while(n) {
if(n & 1) {
res *= x;
}
n >>>= 1;
x *= x;
}
return res;
};
class Solution {
public double myPow(double x, int n) {
long b = n;//INT_MIN的绝对值比INT_MAX大1,会保存不下
double res = 1.0;
if(b < 0){
b = -b;
x = 1/x;
}
while(b > 0){
if((b&1) == 1){
res = res * x;
}
x = x * x;
b = b>>1;
}
return res;
}
}
class Solution {
public:
double myPow(double x, int n) {
//快速幂的方法
double ans = 1;
//处理负数
long y = n;//这里不能用int,因为INT_MIN的绝对值比INT_MAX大1,会保存不下
if(y < 0){
y = -y;
x = 1/x;
}
while(y > 0){
if(y & 1 == 1){// y % 2 == 1
ans = ans * x;
}
x *= x;// x^1 x^2 x^4 x^8
y = y >> 1;// y /= 2;
}
return ans;
}
};
func myPow(x float64, n int) float64 {
var sum float64 = 1
var tmp float64 = x
var flag bool = false
if n < 0 {
n = -n
flag = true
}
for n != 0 {
if n & 1 == 1 {
sum *= tmp
}
tmp *= tmp
n = n >> 1
}
if flag == true {
sum = 1/sum // 负数幂
}
return sum
}
共以下 21 个回答
“`c++
代码
class Solution {
public:
double myPow(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
if (n == -1) return 1 / x;
return myPow(x * x, n >> 1) * myPow(x, n & 1);
}
};
c++ 位运算真的强
class Solution {
public:
double myPow(double x, int n) {
if (n 0) return 1;
if (n 1) return x;
if (n -1) return 1 / x;
double h = myPow(x, n >> 1);
double m = myPow(x, n & 1);
return h * h * m;
}
};
”’java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ret = 0;
while(n != 0){
n &= n-1;//将二进制位中最低位的1变为0之后的结果
ret++;
}
”’
时间复杂度O(n)
空间复杂度O(n)
时间复杂度:O(logn)
class Solution {
public double myPow(double x, int n) {
if(n<0)
{
x=1/x;
n=-1n;
}
double result = 1;
//二进制思路 n变为2进制 如 0101 用每一位2^位能表示回十进制
while(n!=0)
{
if((n&1)1)
{
result=x; //说明这一位可以计算
}
x=x;//不断乘积
n>>>=1;
}
return result;
}
代码
快速幂的思想,思考时通过转换成2进制好理解
快速幂
7.24
Day16:下面是代码
快速幂
快速幂