python3 按位异或 按位与

不用加减乘除做加法

http://www.nowcoder.com/questionTerminal/59ac416b4b944300b617d4f7f111b215

# -*- coding:utf-8 -*-
class Solution:
    def Add(self, num1, num2):
        # write code here
        while num2 != 0:
            temp = num1 ^ num2    # 保存不进位加法值
            num2 = (num1 & num2) << 1    # 保存进位值
            num1 = temp    
        return num1

线上测试的话超时,但是逻辑是没问题的

全部评论
你没考虑负数吧
1 回复 分享
发布于 2020-03-08 18:32
python整型int位数是动态的
点赞 回复 分享
发布于 2021-03-10 18:17

相关推荐

10-31 22:23
门头沟学院 Java
天然不是卷王:太好了 佬的金九银十结束,等offer吐出来,我的金11银12就要开始了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务