美文网首页
LeetCode_Python(2)_两数相加

LeetCode_Python(2)_两数相加

作者: 惑也 | 来源:发表于2018-12-31 21:57 被阅读36次

    需求

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

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

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

    示例:
    输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
    输出:7 -> 0 -> 8
    原因:342 + 465 = 807

    解决思路

    1. 获取2个链表,分别进行遍历,通过借位(个十百千万)进行计算,求出各自的和;
    2. 将2个和相加得到总和,转化为字符串后,通过倒序切片进行反转顺序;
    3. 遍历总和,并添加到实例化链表。

    参考代码

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            n = l1
            i = 1
            num_l1 = 0
            # get num of l1
            while n:
                num_l1 = num_l1 + n.val * i
                i = i * 10
                n = n.next
    
            m = l2
            j = 1
            num_l2 = 0
            # get num of l2
            while m:
                num_l2 = num_l2 + m.val * j
                j = j * 10
                m = m.next
    
            str_num = str(num_l1 + num_l2) 
            str_num = str_num[::-1]
    
            res = list_result = ListNode(0)
            for s in str_num:
                list_result.next = ListNode(int(s))
                list_result = list_result.next          
            return res.next
    

    相关文章

      网友评论

          本文标题:LeetCode_Python(2)_两数相加

          本文链接:https://www.haomeiwen.com/subject/creblqtx.html