不用加号的加法

# -*- coding: utf-8 -*-
#!/usr/bin/env python

"""
Created on Fri Jul 24 13:31:55 2020

@author: MRN_6

@github: https://github.com/WowlNAN

@blog: https://blog.csdn.net/qq_21264377

"""
'''
面试题 17.01. 不用加号的加法
设计一个函数把两个数字相加。不得使用 + 或者其他算术运算符。

示例:

输入: a = 1, b = 1
输出: 2
 

提示:

a, b 均可能是负数或 0
结果不会溢出 32 位整数
From leetcode
'''


class Solution:
    def add(self, a: int, b: int) -> int:
        na=int(a<0)
        nb=int(b<0)
        if na:
            a=int(str(a)[1:])
        if nb:
            b=int(str(b)[1:])
        negative=0
        if na and na^nb and a>b:
            negative=1
        elif na and na^nb and a<b:
            t=a
            a=b
            b=t
        elif nb and na^nb and b>a:
            negative=1
            t=a
            a=b
            b=t
        c=a&1
        d=str(c)
        c=a>>1
        while c>0:
            d+=str(c&1)
            c=c>>1
        c=b&1
        e=str(c)
        c=b>>1
        while c>0:
            e+=str(c&1)
            c=c>>1
        l1=len(d)
        l2=len(e)
        i=0
        carry=0
        r=''
        me=0
        while i<l1 and i<l2:
            m=int(d[i])
            n=int(e[i])
            if na==nb:
                r+=str((m^n)^carry) # both positive or negative
                carry=(m&n)|(m&carry)|(n&carry) # both positive or negative
            else:
                me=(m^n)^carry
                r+=str(me)
                carry=int(m<n or (carry==1 and m==n))
            i+=1
        while i<l1:
            m=int(d[i])
            if na==nb:
                r+=str(m^carry)# both positive or negative
                carry=m&carry# both positive or negative
            else:
                me=m^carry
                r+=str(me)
                carry=int(m<carry)
            i+=1
        if l1>l2 and carry:
            if na==nb:
                r+='1'
        while i<l2:
            n=int(e[i])
            if na==nb:
                r+=str(n^carry) # both positive or negative
                carry=n&carry # both positive or negative
            i+=1
        if l1<=l2 and carry:
            if na==nb:
                r+='1'
        l=len(r)
        ret=''
        for i in range(l):
            ret+=r[l-i-1]
        if na&nb or (na^nb and negative):
            ret='-'+ret
        print(ret, 'ret')
        return int(ret,2)
    
print(Solution().add(-65, 122))
#print(Solution().add(-146746467446746734674673467346717347474747474784787844784783478347873443463644646433464662363635353535335737373737276372676134623645353552342435647575875685686856746373636367327627438746237423462746274637463746274637467234623746273462742647263472647263472346723462374627462746274623746237462746237462178439128476252754275472354712654276458127346527452746257465123841524782631, 23984729412876283462847628746284762462374512735266236523535235235353535353533535235525222727272727272727274636564545354343636345473537473637427722727271))

leetcode中国站上的这道题难度是“简单”。数学逻辑不难。不过,真的简单吗?

按照笔者的代码思路,依稀记得是高等教育计算机系教材上的经典案例吧。仔细看了提交范例,没有发现有用这种思维的。不知道这代码效率咋样)

猜你喜欢

转载自blog.csdn.net/qq_21264377/article/details/107563876