本文共 1008 字,大约阅读时间需要 3 分钟。
discuss中的优秀方法:
def addTwoNumbers(self, l1, l2): carry = 0; res = n = ListNode(0); while l1 or l2 or carry: if l1: carry += l1.val l1 = l1.next; if l2: carry += l2.val; l2 = l2.next; carry, val = divmod(carry, 10) n.next = n = ListNode(val); return res.next;
1、divmod(a,b)使用方法:
python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)
>>>divmod(7, 2)
(3, 1)
2、连等的实际效果:
The assignments are executed left-to-right so that i = arr[i] = f()
evaluates the expression f()
, then assigns the result to the leftmost target, i
, and then assigns the same result to the next target, arr[i]
, using the new value of i
关于本道题的连等
n.next = n = ListNode(val) means first n.next = ListNode(val) then n point to the same address
n = n.next = ListNode(val) means first n = ListNode(val) , now the n is ListNode(val), then n.next point to the address ListNode(val) which means point to itself!!!
转载地址:http://wbrbb.baihongyu.com/