{"id":80,"date":"2021-12-12T19:10:30","date_gmt":"2021-12-12T11:10:30","guid":{"rendered":"http:\/\/139.9.1.231\/?p=80"},"modified":"2021-12-12T20:17:33","modified_gmt":"2021-12-12T12:17:33","slug":"leetcodeday2","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/12\/leetcodeday2\/","title":{"rendered":"leetcodeday2 \u4e24\u6570\u76f8\u52a0"},"content":{"rendered":"\n<h1>\u4e24\u6570\u76f8\u52a0<\/h1>\n\n\n\n<p>\u7ed9\u4f60\u4e24\u4e2a&nbsp;<strong>\u975e\u7a7a<\/strong>&nbsp;\u7684\u94fe\u8868\uff0c\u8868\u793a\u4e24\u4e2a\u975e\u8d1f\u7684\u6574\u6570\u3002\u5b83\u4eec\u6bcf\u4f4d\u6570\u5b57\u90fd\u662f\u6309\u7167&nbsp;<strong>\u9006\u5e8f<\/strong>&nbsp;\u7684\u65b9\u5f0f\u5b58\u50a8\u7684\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8282\u70b9\u53ea\u80fd\u5b58\u50a8&nbsp;<strong>\u4e00\u4f4d<\/strong>&nbsp;\u6570\u5b57\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u5c06\u4e24\u4e2a\u6570\u76f8\u52a0\uff0c\u5e76\u4ee5\u76f8\u540c\u5f62\u5f0f\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u548c\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u4e86\u6570\u5b57 0 \u4e4b\u5916\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4e0d\u4f1a\u4ee5 0&nbsp;\u5f00\u5934\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u5165\uff1al1 = [2,4,3], l2 = [5,6,4]\n\u8f93\u51fa\uff1a[7,0,8]\n\u89e3\u91ca\uff1a342 + 465 = 807.\n\u8f93\u5165\uff1al1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]\n\u8f93\u51fa\uff1a[8,9,9,9,0,0,0,1]\n\u8f93\u5165\uff1al1 = [0], l2 = [0]\n\u8f93\u51fa\uff1a[0]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u6bcf\u4e2a\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u6570\u5728\u8303\u56f4&nbsp;<code>[1, 100]<\/code>&nbsp;\u5185<\/li><li><code>0 &lt;= Node.val &lt;= 9<\/code><\/li><li>\u9898\u76ee\u6570\u636e\u4fdd\u8bc1\u5217\u8868\u8868\u793a\u7684\u6570\u5b57\u4e0d\u542b\u524d\u5bfc\u96f6<\/li><\/ul>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a\u6a21\u62df<br>\n\u601d\u8def\u4e0e\u7b97\u6cd5<\/p>\n\n\n\n<p>\u7531\u4e8e\u8f93\u5165\u7684\u4e24\u4e2a\u94fe\u8868\u90fd\u662f\u9006\u5e8f\u5b58\u50a8\u6570\u5b57\u7684\u4f4d\u6570\u7684\uff0c\u56e0\u6b64\u4e24\u4e2a\u94fe\u8868\u4e2d\u540c\u4e00\u4f4d\u7f6e\u7684\u6570\u5b57\u53ef\u4ee5\u76f4\u63a5\u76f8\u52a0\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u94fe\u8868\uff0c\u9010\u4f4d\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\uff0c\u5e76\u4e0e\u5f53\u524d\u4f4d\u7f6e\u7684\u8fdb\u4f4d\u503c\u76f8\u52a0\u3002\u5177\u4f53\u800c\u8a00\uff0c\u5982\u679c\u5f53\u524d\u4e24\u4e2a\u94fe\u8868\u5904\u76f8\u5e94\u4f4d\u7f6e\u7684\u6570\u5b57\u4e3a n1,n2\u8fdb\u4f4d\u503c\u4e3acarry\uff0c\u5219\u5b83\u4eec\u7684\u548c\u4e3a n1+n2+carry\uff1b\u5176\u4e2d\uff0c\u7b54\u6848\u94fe\u8868\u5904\u76f8\u5e94\u4f4d\u7f6e\u7684\u6570\u5b57\u4e3a=(n1+n2+carry)mod10\uff0c\u800c\u65b0\u7684\u8fdb\u4f4d\u503c\u4e3a\u230an1+n2+carry\u230b\/ 10 \u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e24\u4e2a\u94fe\u8868\u7684\u957f\u5ea6\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u957f\u5ea6\u77ed\u7684\u94fe\u8868\u7684\u540e\u9762\u6709\u82e5\u5e72\u4e2a 00 \u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5982\u679c\u94fe\u8868\u904d\u5386\u7ed3\u675f\u540e\uff0c\u6709 carry&gt;0\uff0c\u8fd8\u9700\u8981\u5728\u7b54\u6848\u94fe\u8868\u7684\u540e\u9762\u9644\u52a0\u4e00\u4e2a\u8282\u70b9\uff0c\u8282\u70b9\u7684\u503c\u4e3acarry\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#\n# @lc app=leetcode.cn id=2 lang=python3\n#\n# [2] \u4e24\u6570\u76f8\u52a0\n#\n\n# @lc code=start\n# Definition for singly-linked list.\n#class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n        # \u5f53\u524d\u6307\u9488\uff0c\u7ed3\u679c\u94fe\u8868\n        result = curr = ListNode()\n        # \u8fdb\u4f4d\u9879\n        remainder = 0\n\n        # \u975e\u7a7a\u6ee1\u8db3\u5faa\u73af\u6761\u4ef6\n        while l1 or l2 :\n            x = l1.val if l1 else 0\n            y = l2.val if l2 else 0\n\n            total = x + y + remainder\n\n            curr.next = ListNode(total%10)\n            remainder = total\/\/10\n# \u9632\u6b62\u67d0\u4e00\u94fe\u8868\u5df2\u7ecf\u4e3a\u7a7a\uff0c\u7a7a\u94fe\u8868.next\u4f1a\u62a5\u9519\n            if l1 : \n                l1 = l1.next\n            if l2 : \n                l2 = l2.next\n            curr = curr.next\n\n        if remainder : \n            curr.next = ListNode(remainder)\n        return result.next\n\n        \n# @lc code=end\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn.pixabay.com\/photo\/2021\/12\/07\/14\/00\/river-6853339_960_720.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<iframe loading=\"lazy\" frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=\"330\" height=\"86\" src=\"\/\/music.163.com\/outchain\/player?type=2&amp;id=1813312517&amp;auto=1&amp;height=66\"><\/iframe>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u6570\u76f8\u52a0 \u7ed9\u4f60\u4e24\u4e2a&nbsp;\u975e\u7a7a&nbsp;\u7684\u94fe\u8868\uff0c\u8868\u793a\u4e24\u4e2a\u975e\u8d1f\u7684\u6574\u6570\u3002\u5b83\u4eec\u6bcf\u4f4d\u6570\u5b57\u90fd\u662f\u6309\u7167&nbsp;\u9006\u5e8f &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/12\/leetcodeday2\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday2 \u4e24\u6570\u76f8\u52a0<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/80"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":12,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":100,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/80\/revisions\/100"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}