Leetcode: Pow(x, n) and Summary: 负数补码总结
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: 负数补码总结的更多相关文章
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- LeetCode: pow
Title: https://leetcode.com/problems/powx-n/ 思路:二分.使用递归或者非递归.非递归有点难理解.pow(0,0)=1 递归的方法是将n为负数的用除法解决.有 ...
- leetcode pow(x,n)实现
题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...
- [LeetCode] Pow(x, n)
Implement pow(x, n). 有史以来做过最简单的一题,大概用5分钟ac,我采用fast exponential,这个在sicp的第一章就有描述.思想是:如果n是偶数的话,那么m^n = ...
- 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 ...
- leetcode Pow(doubule x,int n)
今天第一天开通博客,心情还是小激动的 上代码: 方法一:常规递归,x的n次方={xn/2*xn/2 //n为偶 xn/2*xn/2 *x //n为奇数 } ...
- [leetcode]Pow(x, n) @ Python
原题地址:https://oj.leetcode.com/problems/powx-n/ 题意:Implement pow(x, n). 解题思路:求幂函数的实现.使用递归,类似于二分的思路,解法来 ...
- LeetCode: Pow(x, n) 解题报告
Pow(x, n) Implement pow(x, n). SOLUTION 1: 使用二分法. 1. 负数的情况,使用以下的公式转化为求正数power,另外,考虑到MIN_VALUE可能会造成越界 ...
- 剑指offer-int类型负数补码中1的个数-位操作
在java中Interger类型表示的最大数是 System.out.println(Integer.MAX_VALUE);//打印最大整数:2147483647 这个最大整数的二进制表示,头部少了一 ...
随机推荐
- Ubuntu 12.04 Openstack Essex 安装(单节点)
这是陈沙克一篇非常好的博文,当时在进行openstack排错的时候,多亏了这篇文章里面有些内容 帮我找到了问题的所在: 原文:http://www.chenshake.com/ubuntu-12-04 ...
- iOS中self.xxx 和 _xxx 下划线的区别
property (nonatomic,copy) NSString *propertyName; self.propertyName 是对属性的拜访: _propertyName 是对部分变量的拜访 ...
- linux shell中curl 发送post请求json格式问题
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- Spring Cloud Eureka 高可用注册中心
参考:<<spring cloud 微服务实战>> 在微服务架构这样的分布式环境中,各个组件需要进行高可用部署. Eureka Server 高可用实际上就是将自己作为服务向其 ...
- 涨知识,涨知识 :ThinkPHP框架下Where条件查询Mysql数据库某字段是否为空
代码虐我千百遍,我对代码如初恋~ 问题: 查询某字段app_date数据是否为NULL,正常我们实现的办法是: $map['app_data'] = array('eq','null'); $data ...
- 为什么JS事件函数里面都有一个参数(ev)?
因为ev是事件的参数啊!在ev中包含了事件触发时的参数,比如click事件的ev中包含着.e.pageX,e.pageY,keydown事件中包含着ev.keyCode等,在ie中,ev是全局的可以通 ...
- PAT-GPLT L1-039 - 古风排版 - [字符串输入输出]
题目链接:https://www.patest.cn/contests/gplt/L1-039 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standar ...
- CCCC L2-013. 红色警报 连通分量
题解:将问题转化成连通分量.每次失去一座城市,切断其所有的边,算一次现在的连通分量.若增量大于1,则发出警报. 至于如何算连通分量,直接用tarjan模板 坑://我昨天晚上半夜敲的模板,把一个算所有 ...
- Oracle安装部署之命令建库
1.建目录: [oracle@wen ~]$ mkdir $ORACLE_BASE/admin/rezin/{a,b,c,dp}dump -p [oracle@wen ~]$ mkdir $ORACL ...
- Qt 事件系统浅析 (用 Windows API 描述,分析了QCoreApplication::exec()和QEventLoop::exec的源码)(比起新号槽,事件机制是更高级的抽象,拥有更多特性,比如 accept/ignore,filter,还是实现状态机等高级 API 的基础)
事件系统在 Qt 中扮演了十分重要的角色,不仅 GUI 的方方面面需要使用到事件系统,Signals/Slots 技术也离不开事件系统(多线程间).我们本文中暂且不描述 GUI 中的一些特殊情况,来说 ...