{"id":2898,"date":"2020-02-07T19:03:04","date_gmt":"2020-02-07T11:03:04","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2898"},"modified":"2020-02-07T19:03:04","modified_gmt":"2020-02-07T11:03:04","slug":"%e3%80%90leetcode%e3%80%910043-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%b9%98","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2898\/","title":{"rendered":"\u3010LeetCode\u30110043 \u5b57\u7b26\u4e32\u76f8\u4e58"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/multiply-strings\/\">https:\/\/leetcode-cn.com\/problems\/multiply-strings\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def multiply(self, num1, num2):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> num1: str\n        <\/em><strong><em>:type<\/em><\/strong><em> num2: str\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: str\n        \"\"\"\n        <\/em>if num1 == '0' or num2 == '0':\n            return \"0\"\n        m, n = len(num1), len(num2)\n        res = [0 for _ in range(m + n)]\n        for i in range(m - 1, -1, -1):\n            for j in range(n - 1, -1, -1):\n                x, y = int(num1[i]), int(num2[j])\n                res[i + j + 1] += x * y % 10\n                res[i + j] += x * y \/\/ 10  # \u8fdb\u4f4d\n        print(res)\n        # [8, 17, 26, 19, 10, 1] -> [9, 9, 8, 0, 0, 1]\n        # \u4ece\u540e\u5f80\u524d\u904d\u5386, \u5904\u7406\u8fdb\u4f4d\n        for i in range(m + n - 1, 0, -1):\n            carry = res[i] \/\/ 10\n            # if carry:\n            res[i] = res[i] % 10\n            res[i - 1] += carry\n        ans = ''.join([str(x) for x in res]).lstrip('0')\n        return ans\nif __name__ == '__main__':\n    num1 = '123'\n    num2 = '456'\n    print(Solution().multiply(num1, num2))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u5c0f\u65f6\u5019\u5b66\u60e9\u7f5a\u5217\u7684\u7ad6\u5f0f\uff0c\u53d6\u4e00\u4f4d\u548c\u53e6\u4e00\u4e2a\u7684\u6bcf\u4e00\u4f4d\u76f8\u4e58\u5373\u53ef\u3002\u6700\u540e\u5904\u7406\u4e00\u4e0b\u8fdb\u4f4d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/multiply-strings\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1650,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2898"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2898"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2898\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2898"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}