{"id":3300,"date":"2018-07-26T07:40:16","date_gmt":"2018-07-26T14:40:16","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3300"},"modified":"2018-07-26T07:40:36","modified_gmt":"2018-07-26T14:40:36","slug":"leetcode-43-multiply-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-43-multiply-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 43. Multiply Strings"},"content":{"rendered":"<div class=\"question-description__3U1T\">\n<div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given two non-negative integers\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0represented as strings, return the product of\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>, also represented as a string.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> num1 = \"2\", num2 = \"3\"\r\n<strong>Output:<\/strong> \"6\"<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input:<\/strong> num1 = \"123\", num2 = \"456\"\r\n<strong>Output:<\/strong> \"56088\"\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>The length of both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0is &lt; 110.<\/li>\n<li>Both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0contain\u00a0only digits\u00a0<code>0-9<\/code>.<\/li>\n<li>Both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0do not contain any leading zero, except the number 0 itself.<\/li>\n<li>You\u00a0<strong>must not use any built-in BigInteger library<\/strong>\u00a0or\u00a0<strong>convert the inputs to integer<\/strong>\u00a0directly.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Simulate multiplication one digit at a time.<\/p>\n<p>Time complexity: O(l1*l2)<\/p>\n<p>Space complexity: O(l1 + l2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  string multiply(string num1, string num2) {\r\n    const int l1 = num1.length();\r\n    const int l2 = num2.length();\r\n    string ans(l1 + l2, '0');\r\n    for (int i = l1 - 1; i &gt;= 0; --i)\r\n      for (int j = l2 - 1; j &gt;= 0; --j) {\r\n        int sum = (ans[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0');        \r\n        ans[i + j + 1] = (sum % 10) + '0';\r\n        ans[i + j] += sum \/ 10;\r\n      }\r\n    for (int i = 0; i &lt; ans.length(); ++i)\r\n      if (ans[i] != '0' || i == ans.length() - 1) return ans.substr(i);\r\n    return \"\";\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two non-negative integers\u00a0num1\u00a0and\u00a0num2\u00a0represented as strings, return the product of\u00a0num1\u00a0and\u00a0num2, also represented as a string. Example 1: Input: num1 = &#8220;2&#8221;, num2 = &#8220;3&#8221;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,48,47],"tags":[347,31,177,179,4],"class_list":["post-3300","post","type-post","status-publish","format-standard","hentry","category-math","category-simulation","category-string","tag-big-integer","tag-math","tag-medium","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3300"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3300\/revisions"}],"predecessor-version":[{"id":3302,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3300\/revisions\/3302"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}