不用加号的加法(链表)

# -*- 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
'''
'''
if carry==0 and m<n:
    carry=1
elif carry==0 and m>=n:
    carry=0
elif carry==1 and m<n:
    carry=1
elif carry==1 and m>n:
    carry=0
else:
    carry=1
'''

class ListNode:
    
    def __init__(self, v:str):
        self.v=v
        self.next=None

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
        exchange=False
        if na and na^nb and a>b:
            negative=1
        elif na and na^nb and a<b:
            exchange=True
        elif nb and na^nb and b>a:
            negative=1
            exchange=True
        elif na==0 and nb==0 and b>a:
            exchange=True
        elif na&nb and b>a:
            exchange=True
        if exchange:
            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
        afirst=None
        ctemp=None
        for bi in d:
            temp=ListNode(bi)
            if afirst==None:
                afirst=temp
            elif afirst.next==None:
                afirst.next=temp
                ctemp=temp
            else:
                ctemp.next=temp
                ctemp=temp
        c=b&1
        e=str(c)
        c=b>>1
        while c>0:
            e+=str(c&1)
            c=c>>1
        bfirst=None
        ctemp=None
        for bi in e:
            temp=ListNode(bi)
            if bfirst==None:
                bfirst=temp
            elif bfirst.next==None:
                bfirst.next=temp
                ctemp=temp
            else:
                ctemp.next=temp
                ctemp=temp
        carry=0
        r=''
        me=0
        head=afirst
        head2=bfirst
        while head!=None and head2!=None:
            m=int(head.v)
            n=int(head2.v)
            me=(m^n)^carry
            r+=str(me)
            if na==nb:
                carry=(m&n)|(m&carry)|(n&carry) # both positive or negative
            else:
                carry=int(m<n or (carry==1 and m==n))
            head=head.next
            head2=head2.next
        while head!=None:
            m=int(head.v)
            r+=str(m^carry)
            if na==nb:
                carry=m&carry# both positive or negative
            else:
                carry=int(m<carry)
            head=head.next
        if 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
        return int(ret,2)
    
print(Solution().add(996614456,893139330))
#print(Solution().add(-146746467446746734674673467346717347474747474784787844784783478347873443463644646433464662363635353535335737373737276372676134623645353552342435647575875685686856746373636367327627438746237423462746274637463746274637467234623746273462742647263472647263472346723462374627462746274623746237462746237462178439128476252754275472354712654276458127346527452746257465123841524782631, 23984729412876283462847628746284762462374512735266236523535235235353535353533535235525222727272727272727274636564545354343636345473537473637427722727271))

看了提交范例里面比较靠谱的,是利用数学运算规则,结合位运算和逻辑运算。代码简洁

"""
@author: unknown

"""
class Solution:
    def add(self, a: int, b: int) -> int:
        a &= 0xffffffff
        b &= 0xffffffff
        while b!=0:
            c = a&b
            a ^= b
            b = (c<<1)&0xffffffff
        return a if a<0x80000000 else ~(a^0xffffffff)

猜你喜欢

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