題目描述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當(dāng) a=-b 時(shí) 為什么代碼會超時(shí), 而同樣的邏輯,用Java就不會超時(shí) public int aplusb(int a ,int b) { // write your code here, try to do it without arithmetic operators.
while(true){ int x = a^b; //記錄不進(jìn)位數(shù)
int y = (a&b)<<1; //記錄進(jìn)位數(shù)
if(x==0){ return y;
} if (y==0){ return x;
}
a=x;
b=y;
} // while
}
2 回答

躍然一笑
TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
原碼、反碼和補(bǔ)碼
咱整形數(shù)據(jù)
以八位二進(jìn)制
為例
那用整數(shù)5
舉個(gè)例子吧:
原碼: 0000 0101
反碼: 1111 1010
補(bǔ)碼: 1111 1011
而補(bǔ)碼就是負(fù)數(shù)在計(jì)算機(jī)中的二進(jìn)制表示方法
移位操作
<<
和>>
簡單的說就是向左或向右移動指定的位數(shù), 空缺用0或1來補(bǔ), 溢出的部分將被舍棄
添加回答
舉報(bào)
0/150
提交
取消