博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode002. Add Two Numbers
阅读量:2243 次
发布时间:2019-05-09

本文共 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/

你可能感兴趣的文章
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>