{"id":555,"date":"2020-09-26T10:57:07","date_gmt":"2020-09-26T02:57:07","guid":{"rendered":"http:\/\/39.96.58.60\/?p=555"},"modified":"2022-10-18T16:38:54","modified_gmt":"2022-10-18T08:38:54","slug":"leetcode-43-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%b9%98","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=555","title":{"rendered":"Leetcode 43. \u5b57\u7b26\u4e32\u76f8\u4e58"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570&nbsp;<code>num1<\/code>&nbsp;\u548c&nbsp;<code>num2<\/code>\uff0c\u8fd4\u56de&nbsp;<code>num1<\/code>&nbsp;\u548c&nbsp;<code>num2<\/code>&nbsp;\u7684\u4e58\u79ef\uff0c\u5b83\u4eec\u7684\u4e58\u79ef\u4e5f\u8868\u793a\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> num1 = \"2\", num2 = \"3\"\n\n<strong>\u8f93\u51fa:<\/strong> \"6\"<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> num1 = \"123\", num2 = \"456\"\n\n<strong>\u8f93\u51fa:<\/strong> \"56088\"<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ol>\n<li><code>num1<\/code>&nbsp;\u548c&nbsp;<code>num2<\/code>&nbsp;\u7684\u957f\u5ea6\u5c0f\u4e8e110\u3002<\/li>\n<li><code>num1<\/code> \u548c&nbsp;<code>num2<\/code> \u53ea\u5305\u542b\u6570\u5b57&nbsp;<code>0-9<\/code>\u3002<\/li>\n<li><code>num1<\/code> \u548c&nbsp;<code>num2<\/code>&nbsp;\u5747\u4e0d\u4ee5\u96f6\u5f00\u5934\uff0c\u9664\u975e\u662f\u6570\u5b57 0 \u672c\u8eab\u3002<\/li>\n<li><strong>\u4e0d\u80fd\u4f7f\u7528\u4efb\u4f55\u6807\u51c6\u5e93\u7684\u5927\u6570\u7c7b\u578b\uff08\u6bd4\u5982 BigInteger\uff09<\/strong>\u6216<strong>\u76f4\u63a5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u6765\u5904\u7406<\/strong>\u3002<\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u5b66\u3001 \u5b57\u7b26\u4e32\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a244 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8628.18% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8613.10% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6a21\u62df\u7ad6\u5f0f\u4e58\u6cd5\u8fd0\u7b97\uff0c\u903b\u8f91\u4e0e\u5217\u7ad6\u5f0f\u76f8\u540c\n\"\"\"\nclass Solution:\n    def multiply(self, num1: str, num2: str) -&gt; str:\n        result = 0\n        for i, val1 in enumerate(num1[::-1]):\n            for j, val2 in enumerate(num2[::-1]):\n                result += int(val1) * int(val2) * 10 ** (i + j)\n        return str(result)\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a124 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8660.60% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8617.70% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u4f18\u5316\u540e\uff0c\u901f\u5ea6\u5927\u5e45\u63d0\u5347\uff0c\u4f7f\u7528\u6570\u7ec4\u5b58\u50a8\u6bcf\u6b21\u7684\u8ba1\u7b97\u7ed3\u6784\uff0c\u7136\u540e\u6c42\u548c    \n\"\"\"\nclass Solution:\n    def multiply(self, num1: str, num2: str) -&gt; str:\n        if num1 == \"0\" or num2 == \"0\":\n            return \"0\"\n\n        record = [0]*(len(num1)+len(num2))\n        for i, val1 in enumerate(num1[::-1]):\n            for j, val2 in enumerate(num2[::-1]):\n                record[i+j] += int(val1) * int(val2)\n\n        result = 0\n        for power, val in enumerate(record):\n            result += val*10**power\n\n        return str(result)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570&nbsp;num1&nbsp;\u548c&nbsp;num2\uff0c\u8fd4\u56de&nbsp;nu&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/555"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=555"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":556,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions\/556"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}