LeetCode2. python实现:两数相加实例☆☆

问题:

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:

[2,4,3]
[5,6,4]

输出:

[7,0,8]

预期结果:

[7,0,8]

#  基于链表直接输出结果(执行速度很快)

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    
    # 直接基于链表输出结果
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        p,q = l1,l2
        curr = ListNode(0)
        result = curr
        jinzhi = 0
        while(p or q):
            if p:x = p.val
            else:x = 0
            if q:y = q.val
            else:y = 0
                
            temp = x+y+jinzhi
            jinzhi = temp//10
            curr.next = ListNode(temp%10)
            curr = curr.next
            
            if p:p = p.next
            if q:q = q.next
        if jinzhi >0:
            curr.next = ListNode(jinzhi)
        return result.next


    
    # 直接基于链表输出结果(和上面大体思想是一样的,但执行效率很低)

    def addTwoNumbers2(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        res = [] # 存储输出结果列表
        next1 = l1.next
        next2 = l2.next
        value1 = l1.val
        value2 = l2.val
        temp = value1+value2
        jinzhi = 0
        if temp>9:
            jinzhi = 1
            temp = temp%10
         
        res.append(temp)
        while next1 or next2:
            if next1 and next2:
                temp = next1.val +next2.val +jinzhi
                if temp >9:
                    jinzhi=1
                    temp = temp%10
                else:
                    jinzhi = 0
                res.append(temp)
                
                next1 = next1.next
                next2 = next2.next
            elif next1 and next2 is None:
                temp = next1.val + jinzhi
                if temp >9:
                    jinzhi=1
                    temp = temp%10
                else:
                    jinzhi =0
                res.append(temp)
            elif next2 and next1 is None:
                temp = next2.val + jinzhi
                if temp >9:
                    jinzhi=1
                    temp = temp%10
                else:
                    jinzhi = 0
                res.append(temp)
        if jinzhi==1:
            res.append(jinzhi)
        return res


     # 直接基于链表输出结果 (鉴于addTwoNumbers(),修改的addTwoNumbers2(),实现效率和addTwoNumbers()一样)

    # 直接基于链表输出结果          
    def addTwoNumbers3(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        res = [] # 存储输出结果列表
        next1 = l1
        next2 = l2
        jinzhi = 0

        while next1 or next2:
            if next1 and next2:
                temp = next1.val +next2.val +jinzhi

                next1 = next1.next
                next2 = next2.next
            elif next1 and next2 is None:
                temp = next1.val + jinzhi
                next1 = next1.next

            elif next2 and next1 is None:
                temp = next2.val + jinzhi
                next2 = next2.next
            
            jinzhi = temp//10
            temp = temp%10
            res.append(temp)
        if jinzhi==1:
            res.append(jinzhi)
        return res

           
                
    
    # 先得到两个链表的值,得到和后,在取余输出结果(相比addTwoNumbers2()好点,但仍然很高)
   

def addTwoNumbers4(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        value1 = l1.val
        i=1
        next1 = l1.next
        while next1!=None:
            value1 = value1 +next1.val*(10**i)
            i = i+1
            next1 = next1.next
                
        value2 = l2.val
        i=1
        next2 = l2.next
        while next2!=None:
            value2 = value2 +next2.val*(10**i)
            i = i+1
            next2 = next2.next
        result = value1+value2
        
        res = []  # 存储数据结果列表
        while result >9:
            res.append(result%10) 
            result = result//10
        res.append(result)
        return res

猜你喜欢

转载自blog.csdn.net/weixin_42521211/article/details/87937113
今日推荐