Implement pow(x, n).

Analysis:  Time Complexity: O(LogN)

Iterative code: refer to https://discuss.leetcode.com/topic/40546/iterative-log-n-solution-with-clear-explanation

N = 9 = 2^3 + 2^0 = 1001 in binary. Then:

x^9 = x^(2^3) * x^(2^0)

We can see that every time we encounter a 1 in the binary representation of N, we need to multiply the answer with x^(2^i) where i is the ith bit of the exponent. Thus, we can keep a running total of repeatedly squaring x - (x, x^2, x^4, x^8, etc) and multiply it by the answer when we see a 1.

 public class Solution {
public double MyPow(double x, int n) {
double ans = 1;
long absN = Math.Abs((long)n);
while(absN > 0) {
if((absN&1)==1) ans *= x;
absN >>= 1;
x *= x;
}
return n < 0 ? 1/ans : ans;
}
}

Recursive code:

 double pow(double x, int n) {
if (n == 0) return 1.0;
double half = pow(x, n/2);
if (n%2 == 0)
{
return half*half;
}
else if (n>0)
{
return half*half*x;
}
else
{
return half/x*half;
}
}

这道题其实细细玩味起来有不少值得注意的地方:位运算的细节。

我第一遍代码里面其实有不妥的地方,比如第4行的1/helper(x, -n),当n取Integer.MIN_VALUE时,取反结果还是自己

JAVA中整数采用符号二进制补码表示(Two's Complement),补码对正数没什么好说的,但对负数还是要注意。

比如:

-8  1000    -4  1100    0  0000    4  0100

-7  1001    -3  1101    1  0001    5  0101

-6  1010    -2  1110    2  0010    6  0110

-5  1011    -1  1111    3  0011    7  0111

一旦某一端overflow, 就会跑到另外一端去wrap up,这就是为什么Integer.MAX_VALUE+1 会是Integer.MIN_VALUE

Integer.MIN_VALUE,即-2147483648,二进制位如下:

1000 0000 0000 0000 0000 0000 0000 0000

在计算机的运算中,“-”(前缀)运算表示各二制位取反再加1,也就是说 b = -a 在计算机内部是 b = ~a + 1 这样处理的,所以上面的位就变成了:

   1000 0000 0000 0000 0000 0000 0000 0000 Integer.MIN_VALUE

取反 0111 1111 1111 1111 1111 1111 1111 1111 (取反之后变成了Integer.MAX_VALUE)

加1 1000 0000 0000 0000 0000 0000 0000 0000 -Integer.MIN_VALUE(与原来的结果一样)

这就导致了我如下的一段code stackoverflow了

 public class Solution {
public double pow(double x, int n) {
if (n < 0) return 1/pow(x, Math.abs(n));
if (n == 0) return 1.0;
else {
double half = pow(x, n/2);
if (n % 2 == 0) return half * half;
else return half * half * x;
}
}
}

java.lang.StackOverflowError     last input: 1.00000, -2147483648, 因为-2147483648取反永远是自己,好像0取反永远是自己一样。我第一遍的code也只是侥幸结果对,其实并不好。还是像第二遍code那样写比较好

Leetcode: Pow(x, n) and Summary: 负数补码总结的更多相关文章

  1. [LeetCode] Pow(x, n) 求x的n次方

    Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...

  2. LeetCode: pow

    Title: https://leetcode.com/problems/powx-n/ 思路:二分.使用递归或者非递归.非递归有点难理解.pow(0,0)=1 递归的方法是将n为负数的用除法解决.有 ...

  3. leetcode pow(x,n)实现

    题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...

  4. [LeetCode] Pow(x, n)

    Implement pow(x, n). 有史以来做过最简单的一题,大概用5分钟ac,我采用fast exponential,这个在sicp的第一章就有描述.思想是:如果n是偶数的话,那么m^n = ...

  5. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  6. leetcode Pow(doubule x,int n)

    今天第一天开通博客,心情还是小激动的 上代码: 方法一:常规递归,x的n次方={xn/2*xn/2              //n为偶 xn/2*xn/2 *x          //n为奇数 } ...

  7. [leetcode]Pow(x, n) @ Python

    原题地址:https://oj.leetcode.com/problems/powx-n/ 题意:Implement pow(x, n). 解题思路:求幂函数的实现.使用递归,类似于二分的思路,解法来 ...

  8. LeetCode: Pow(x, n) 解题报告

    Pow(x, n) Implement pow(x, n). SOLUTION 1: 使用二分法. 1. 负数的情况,使用以下的公式转化为求正数power,另外,考虑到MIN_VALUE可能会造成越界 ...

  9. 剑指offer-int类型负数补码中1的个数-位操作

    在java中Interger类型表示的最大数是 System.out.println(Integer.MAX_VALUE);//打印最大整数:2147483647 这个最大整数的二进制表示,头部少了一 ...

随机推荐

  1. Android 验证APK是否已经签名或是否是Debug签名

    https://source.android.google.cn/ http://www.android-doc.com/tools/publishing/app-signing.html Signi ...

  2. Android.mk(5) 计算怎么办?

    https://www.jianshu.com/p/57c01e97c9b8 计算怎么办? 前面我们把Makefile做为一门语言的主要特性大致做了一个描述,它集合了目标式的模式和函数式的模式,还有大 ...

  3. sencha touch 我的公用类myUtil(废弃 仅参考)

    /*公共类*/ Ext.define('myUtil', { statics: { //store公用加载方法 storeLoadById: function (id) { var store = E ...

  4. spring-data-redis的事务操作深度解析--原来客户端库还可以攒够了事务命令再发?

    一.官方文档 简单介绍下redis的几个事务命令: redis事务四大指令: MULTI.EXEC.DISCARD.WATCH. 这四个指令构成了redis事务处理的基础. 1.MULTI用来组装一个 ...

  5. ms转成00:00:00的时间格式化

    毫秒转成 00:00:00的时间格式 比如1000毫秒转成00:00:01 /** * 格式化邀请的时间 * @param time ms */ public static formatTime(ti ...

  6. jdbc(1)(三)DBCP、C3P0、Proxool 、 BoneCP开源连接池的简介

     简介          使用评价  项目主页  DBCP DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序用使用 可以设置最大和最小连 ...

  7. 数字模型制作规范(转自Unity3D群)

    本文提到的所有数字模型制作,全部是用3D MAX建立模型,即使是不同的驱动引擎,对模型的要求基本是相同的.当一个VR模型制作完成时,它所包含的基本内容包括场景尺寸.单位,模型归类塌陷.命名.节点编辑, ...

  8. 【Win】使用L2TP出现809错误

    1.环境:win7/10 2.解决: a.修改注册表(新建一个文本文件,复制以下内容,保存后文件扩展名改为.reg,双击运行文件.) Windows Registry Editor Version 5 ...

  9. yii---定义全局函数

    YII它不像Thinkphp等框架一样,已经有全局函数,YII要使用全局函数需要自己去定义,然后在入口文件中进行引入: 例如:我们看 yii 的入口文件: 看到这里,我们看到有个 autoload.p ...

  10. vue--简单数据绑定

    <template> <div id="app"> {{msg}} //绑定数据 {{obj.name}} //绑定对象 <p v-for=" ...