{"id":2172,"date":"2016-09-09T14:20:07","date_gmt":"2016-09-09T06:20:07","guid":{"rendered":"http:\/\/boweihe.me\/?p=2172"},"modified":"2016-09-09T14:20:07","modified_gmt":"2016-09-09T06:20:07","slug":"leetcode-43-multiply-strings","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2172","title":{"rendered":"LeetCode 43. Multiply Strings"},"content":{"rendered":"<h3>\u6ce8\u610f\/\u601d\u8def<\/h3>\n<ul>\n<li>\u4fdd\u5b58 \u88ab\u4e58\u6570*[0&#8230;9]\u7684\u7ed3\u679c\u5907\u7528\uff0c\u7136\u540e\u79fb\u4f4d\u76f8\u52a0\u5373\u53ef<\/li>\n<li>\u4e58\u6570\u672b\u5c3e\u662f0<\/li>\n<\/ul>\n<h3>\u4ee3\u7801<\/h3>\n<p>9ms<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    void ReverseStr(string&amp; str){\n        int left=0, right = str.size()-1;\n        while(left&lt;right){\n            char c = str[left];\n            str[left] = str[right];\n            str[right] = c;\n            left++;\n            right--;\n        }\n    }\n    void AddResult(string&amp; result, const string&amp; subProduct, const int shift){\n        int carry = 0;\n        while(result.size() &lt; shift){\n            result += \"0\";\n        }\n        for(int i=shift; i&lt;subProduct.size() + shift; i++){\n            if(i &lt; result.size()){\n                int subSum = (result[i]-'0') + (subProduct[i-shift]-'0') + carry;\n                if(subSum &gt;= 10){\n                    subSum-= 10;\n                    carry = 1;\n                } else {\n                    carry = 0;\n                }\n                result[i] = subSum + '0';\n            } else {\n                int subSum = (subProduct[i-shift]-'0') + carry;\n                carry = subSum \/ 10;\n                subSum = subSum % 10;\n                result = result + (char)(subSum+'0');\n            }\n        }\n        if (carry &gt; 0) {\n\t\t\tresult += (char)(carry + '0');\n\t\t}\n    }\n    string getSubProduct(char digit, string op){\n        int carry = 0;\n        int d = digit-'0';\n        for(int i=0; i&lt;op.size(); i++){\n            int prod = (op[i]-'0') * d + carry;\n            carry = prod \/ 10;\n            prod = prod % 10;\n            op[i] = '0' + prod;\n        }\n        if(carry &gt; 0){\n            op = op + (char)(carry+'0');\n        }\n        return op;\n    }\n    string multiply(string op1, string op2) {\n        string result;\n        if(op1.empty() || op2.empty())\n            return result;\n        if(op1.size() &lt; op2.size()){\n            string temp = op1;\n            op1 = op2;\n            op2 = temp;\n        }\n        string productSub[10];\n        ReverseStr(op1);\n        ReverseStr(op2);\n        for(int i=0; i&lt;op2.size(); i++){\n            char digit = op2[i];\n            if(digit == '0')\n                continue;\n            if(productSub[digit-'0'].empty()){\n                productSub[digit-'0'] = getSubProduct(digit, op1);\n            }\n            AddResult(result, productSub[digit-'0'], i);\n        }\n        if(result.empty())\n            return \"0\";\n        ReverseStr(result);\n        return result;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u610f\/\u601d\u8def \u4fdd\u5b58 \u88ab\u4e58\u6570*[0&#8230;9]\u7684\u7ed3\u679c\u5907\u7528\uff0c\u7136\u540e\u79fb\u4f4d\u76f8\u52a0\u5373\u53ef \u4e58\u6570\u672b\u5c3e\u662f0 \u4ee3\u7801 9ms class Solution { public: void ReverseStr(string&amp; str){ int left=0, right = str.size()-1; while(left&lt;right){ char c = str[left]; str[left] = str[right]; str[right] = c; left++; right&#8211;; } } void AddResult(string&amp; result, const string&amp; subProduct, const int shift){ int carry = 0; while(result.size() &lt; shift){ result += &#8220;0&#8221;; } for(int i=shift; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,151],"class_list":["post-2172","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-151"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2172"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2172\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}