zoukankan      html  css  js  c++  java
  • leetcode——2. 两数相加

    第一次解除到链表相关的题目,不会做,看了别人答案。

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            n=l1.val+l2.val
            l3=ListNode(n%10)
            l3.next=ListNode(n//10)
            p1=l1.next
            p2=l2.next
            p3=l3
            while True:
                if p1 and p2:
                    sum=p1.val+p2.val+p3.next.val
                    p3.next.val=sum%10
                    p3.next.next=ListNode(sum//10)
                    p1=p1.next
                    p2=p2.next
                    p3=p3.next
                elif p1 and not p2:
                    sum=p1.val+p3.next.val
                    p3.next.val=sum%10
                    p3.next.next=ListNode(sum//10)
                    p1=p1.next
                    p3=p3.next
                elif not p1 and p2:
                    sum=p2.val+p3.next.val
                    p3.next.val=sum%10
                    p3.next.next=ListNode(sum//10)
                    p2=p2.next
                    p3=p3.next
                else:
                    if p3.next.val==0:
                        p3.next=None
                    break
            return l3
    执行用时 :108 ms, 在所有 python3 提交中击败了32.62%的用户
    内存消耗 :13.8 MB, 在所有 python3 提交中击败了5.06%的用户
     
    不求其他,看懂就好。
    执行用时为 52 ms 的范例
    # Definition for singly-linked list.
    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            if l1 is None:
                return l2
            if l2 is None:
                return l1
            carry = 0
            head = ListNode(0)
            node = head
            while l1 and l2:
                sum1 = l1.val + l2.val + carry
                carry = sum1//10
                tmp = sum1%10
                head.next = ListNode(tmp)
                head = head.next
                l1 = l1.next
                l2 = l2.next
            while l1:
                sum1 = l1.val+carry
                carry = sum1//10
                tmp = sum1%10
                head.next = ListNode(tmp)
                head = head.next
                l1 = l1.next
            while l2 :
                sum1 = l2.val + carry
                carry = sum1//10
                tmp = sum1%10
                head.next = ListNode(tmp)
                head = head.next
                l2 = l2.next
            if carry:
                head.next = ListNode(carry)
                head = head.next
            return node.next

                                                                                                           ——2019.10.23

     
     完整版
    # Definition for singly-linked list.
    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            prenode = ListNode(0)
            lastnode = prenode
            val = 0
            while val or l1 or l2:
                val, cur = divmod(val + (l1.val if l1 else 0) + (l2.val if l2 else 0), 10)
                lastnode.next = ListNode(cur)
                lastnode = lastnode.next
                l1 = l1.next if l1 else None
                l2 = l2.next if l2 else None
            return prenode.next
    
    
    def generateList(l: list) -> ListNode:
        prenode = ListNode(0)
        lastnode = prenode
        for val in l:
            lastnode.next = ListNode(val)
            lastnode = lastnode.next
        return prenode.next
    
    def printList(l: ListNode):
        while l:
            print("%d, " %(l.val), end = '')
            l = l.next
        print('')
    
    if __name__ == "__main__":
        l1 = generateList([1, 5, 8])
        l2 = generateList([9, 1, 2, 9])
        printList(l1)
        printList(l2)
        s = Solution()
        sum = s.addTwoNumbers(l1, l2)
        printList(sum)
    
    作者:xiao-lin-20
    链接:https://leetcode-cn.com/problems/add-two-numbers/solution/cjie-ti-de-wan-zheng-dai-ma-bao-gua-sheng-cheng-ce/
    来源:力扣(LeetCode)

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode head = new ListNode(-1);
            ListNode node = head;
            int v = 0;
            while(l1!=null && l2!= null){
                node.next = new ListNode(( v + l1.val + l2.val)%10);
                v = (l1.val + l2.val + v)/10;
                l1 = l1.next;
                l2 = l2.next;
                node = node.next;
            }
            while (l1 != null){
                node.next = new ListNode(( v + l1.val)%10);
                v = (l1.val+v)/10;
                l1 = l1.next;
                node = node.next;
            }
            while (l2 != null){
                node.next = new ListNode(( v + l2.val)%10);
                v = (l2.val+v)/10;
                l2 = l2.next;
                node = node.next;
            }
            if(v != 0){
                node.next = new ListNode(v);
            }
            return head.next;
        }

     ——2020.7.13

     
     
     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    ORACLE 计算时间相减间隔
    oracle中游标详细用法
    oracle中计算某月的天数
    Unity3D导出的EXE不用显示分辨率选择界面
    Unity3D 之暂停和继续的实现
    double的值太大,以及补0
    Unity3D鼠标点击物体产生事件
    java POi excel 写入大批量数据
    Unity3D 判断鼠标是否按在UGUI上
    Unity3D 之UGUI 滚动条
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11727282.html
Copyright © 2011-2022 走看看