48.不用加减乘除做加法(python)
题目描述
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
java
public class Solution { public int Add(int num1,int num2) { while (num2!=0) { int temp = num1^num2; num2 = (num1&num2)<<1; num1 = temp; } return num1; } }
python
class Solution: def Add(self, num1, num2): # write code here while num2!=0: sum_ = num1 ^ num2 carry = (num1 & num2)<<1 num1=sum_%0x100000000 num2=carry%0x100000000 if num1<=0x7FFFFFFF: return num1 else: return num1 |(~0x100000000+1)
相关推荐
cuixingwudi 2020-07-26
数据与算法之美 2020-07-05
路漫 2020-06-26
earthhouge 2020-06-25
yunfeitian 2020-06-13
klarclm 2020-06-13
zhixingheyitian 2020-05-28
qscool 2020-05-17
cuiweisaidelike 2020-05-11
winmeanyoung 2020-05-08
alicelmx 2020-05-05
一只码畜 2020-04-22
拉斯厄尔高福 2020-04-18
muhongdi 2020-04-18
choupiaoyi 2020-04-06
wbczyh 2020-03-28
jeonkc 2020-03-26
狼窝 2020-03-20