为了账号安全,请及时绑定邮箱和手机立即绑定

python 解决lintcode a+b 超时问题

python 解决lintcode a+b 超时问题

皈依舞 2019-03-07 16:27:16
题目描述lintcode a+b 问题题目来源及自己的思路https://www.lintcode.com/prob...代码def aplusb(self, a, b):     # write your code here     while True:         a, b = a^b, (a&b)<<1         if a==0 or b == 0:            return a or b当 a=-b 时 为什么代码会超时, 而同样的逻辑,用Java就不会超时  public int aplusb(int a ,int b) {        // write your code here, try to do it without arithmetic operators.         while(true){        int x = a^b;  //记录不进位数         int y = (a&b)<<1;  //记录进位数         if(x==0){            return y;         }        if (y==0){            return x;         }         a=x;         b=y;         } // while                      }
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

原码、反码和补码

整形数据八位二进制为例
那用整数5举个例子吧:

  • 原码: 0000 0101

  • 反码: 1111 1010

  • 补码: 1111 1011

补码就是负数在计算机中的二进制表示方法

移位操作

<<>>简单的说就是向左或向右移动指定的位数, 空缺用0或1来补, 溢出的部分将被舍弃


查看完整回答
反对 回复 2019-03-07
  • 2 回答
  • 0 关注
  • 865 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信