{"id":6040,"date":"2024-04-18T00:09:05","date_gmt":"2024-04-17T16:09:05","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=6040"},"modified":"2024-04-18T00:09:05","modified_gmt":"2024-04-17T16:09:05","slug":"43-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%b9%98","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/18\/43-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%b9%98\/","title":{"rendered":"43. \u5b57\u7b26\u4e32\u76f8\u4e58"},"content":{"rendered":"<p><a href=\"https:\/\/leetcode.cn\/problems\/multiply-strings\/\">43. \u5b57\u7b26\u4e32\u76f8\u4e58<\/a><\/p>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570 <code>num1<\/code> \u548c <code>num2<\/code>\uff0c\u8fd4\u56de <code>num1<\/code> \u548c <code>num2<\/code> \u7684\u4e58\u79ef\uff0c\u5b83\u4eec\u7684\u4e58\u79ef\u4e5f\u8868\u793a\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u4e0d\u80fd\u4f7f\u7528\u4efb\u4f55\u5185\u7f6e\u7684 BigInteger \u5e93\u6216\u76f4\u63a5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code>\u8f93\u5165: num1 = &quot;2&quot;, num2 = &quot;3&quot;\n\u8f93\u51fa: &quot;6&quot;<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code>\u8f93\u5165: num1 = &quot;123&quot;, num2 = &quot;456&quot;\n\u8f93\u51fa: &quot;56088&quot;<\/code><\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= num1.length, num2.length &lt;= 200<\/code><\/li>\n<li><code>num1<\/code> \u548c <code>num2<\/code> \u53ea\u80fd\u7531\u6570\u5b57\u7ec4\u6210\u3002<\/li>\n<li><code>num1<\/code> \u548c <code>num2<\/code> \u90fd\u4e0d\u5305\u542b\u4efb\u4f55\u524d\u5bfc\u96f6\uff0c\u9664\u4e86\u6570\u5b570\u672c\u8eab\u3002<\/li>\n<\/ul>\n<h3>1\u3001\u89e3\u9898\u524d\u7f6e\u77e5\u8bc6<\/h3>\n<p>1\u3001<strong>\u4e24\u4e2a\u957f\u5ea6\u5206\u522b\u4e3a <code>n<\/code> \u548c <code>m<\/code> \u7684\u6570\u76f8\u4e58\uff0c\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 <code>n + m<\/code>\u3002<\/strong><\/p>\n<p>2\u3001\u6211\u4eec\u8981\u4f7f\u7528ASIC\u7801\u6765\u8fdb\u884c\u76f8\u52a0<\/p>\n<ul>\n<li>eg\uff1aint data2 = num2.charAt(j)-'0'<\/li>\n<li>\u20180\u2019\u662f48\u4f9d\u6b21\u5f80\u540e<\/li>\n<\/ul>\n<p>3\u3001\u7528\/\u6765\u8fdb\u884c\u5224\u65ad\u8fdb\u4f4d\u4e2a\u6570<\/p>\n<p>4\u3001\u7528%\u6765\u53d6\u4f59\u7559\u4e0b\u5269\u4e0b\u7684\u6570<\/p>\n<h3>2\u3001\u89e3\u9898\u601d\u8def<\/h3>\n<p>1\u3001\u6211\u4eec\u76f4\u63a5\u8fdb\u884c\u6a21\u62df\u4e58\u6cd5\u8ba1\u7b97<\/p>\n<p>2\u3001\u6211\u4eec\u7528\u4e00\u4e2a\u6570\u7ec4res\u6765\u5b58\u50a8\u4e24\u6570\u76f8\u4e58\u7684\u7ed3\u679c<\/p>\n<p>3\u3001\u4e58\u5b8c\u540e\u5f97\u5230\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u518d\u8fdb\u884c\u7edf\u4e00\u7684\u5904\u7406<\/p>\n<p>4\u3001\u75281\u4e2afor\u5faa\u73af\uff0c\u6765\u5904\u7406\u8fdb\u4f4d\u548c\u4fdd\u7559\u4e2a\u6570\u4f4d<\/p>\n<p>5\u3001\u7528StringBuilder\u6765\u8fdb\u884c\u8ba1\u7b97\u548c\u53bb\u9664\u524d\u7f6e0<\/p>\n<h3>3\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-java\">package org.letcode;\n\nimport java.util.Arrays;\n\npublic class Num43 {\n\n    public static void main(String[] args) {\n        Num43 num43 = new Num43();\n        System.out.println(num43.multiply3(&quot;123&quot;,&quot;456&quot;));\n\n    }\n\n    public String multiply3(String num1, String num2) {\/\/\u505a\u4e58\u6cd5,\u65f6\u95f4\u590d\u6742\u5ea6O(len1*len2)\n        if(num1.equals(&quot;0&quot;) || num2.equals(&quot;0&quot;)) return &quot;0&quot;;\n        int len1 = num1.length();\n        int len2 = num2.length();\n        int[] res = new int[len1+len2];\/\/\u5206\u522b\u53d6\u6700\u5c0f\u503c\u6700\u5927\u503c\u53ef\u4ee5\u8bc1\u660e,num1*num2\u7684\u7ed3\u679c\u957f\u5ea6\u4e3a len1+len2-1 \u6216 len1+len2\n        Arrays.fill(res,0);\n        for(int i=len1-1;i&gt;=0;i--){\n            int data1 = num1.charAt(i)-&#039;0&#039;;\n            for(int j=len2-1;j&gt;=0;j--){\n                int data2 = num2.charAt(j)-&#039;0&#039;;\n                \/\/data1*data2\u7684\u7ed3\u679c\u4fdd\u7559\u5728i+j+1\u4f4d\u7f6e\u4e0a,\u4e3a\u4ec0\u4e48?\u4ee5i=len1-1,j=len2-1\u90fd\u662f\u6700\u4f4e\u4f4d\u4e3a\u4f8b,\u7ed3\u679c\u4e5f\u5e94\u8be5\u5728\u6700\u4f4e\u4f4dlen1+len2-1\n                res[i+j+1] += data1*data2;\/\/\u5148\u4e0d\u7ba1\u8fdb\u4f4d,\u540e\u9762\u518d\u7edf\u4e00\u5904\u7406\n            }\n        }\n        for (int i = len1+len2-1; i &gt; 0; i--) {\n            if(res[i]&gt;9){\/\/\u67d0\u4f4d\u4e0a\u6570\u5b57\u5927\u4e8e\u7b49\u4e8e10\u4e86\u9700\u8981\u8fdb\u4f4d\n                res[i-1] += res[i]\/10; \/\/\u524d\u4e00\u4e2a\u6570\u52a0\u8fdb\u4f4d\n                res[i] %= 10; \/\/\u5f53\u524d\u4f4d\u53ea\u4fdd\u7559\u4e2a\u4f4d\u6570\n            }\n        }\n        StringBuilder sb = new StringBuilder();\n        if(res[0]!=0) sb.append(res[0]);\/\/\u53ea\u6709\u6700\u9ad8\u4f4d\u4e3a\u4e0d0\u624d\u4fdd\u7559\u5230\u7ed3\u679c\u4e2d\n        for(int i=1;i&lt;len1+len2;i++){\n            sb.append(res[i]);\n        }\n        return sb.toString();\n    }\n\n}\n<\/code><\/pre>\n<h3>4\u3001\u8865\u5145<\/h3>\n<h4>1\u3001String\u8f6cInteger<\/h4>\n<p>1\u3001Integer.parseInt() \u6216 Integer.valueOf() \u5c06 String \u8f6c\u6362\u4e3a int\u3002<\/p>\n<p>2\u3001\u5b9e\u4f8b\uff08Integer.parseInt()\uff09<\/p>\n<pre><code class=\"language-java\">String number = &quot;-7&quot;;\n\n  \/\/ result = -7\n  int result = Integer.parseInt(number);\n  \/\/ result2 = -7\n  Integer result2 = Integer.valueOf(number);<\/code><\/pre>\n<p>3\u3001\u5b9e\u4f8b\uff08 Integer.valueOf()\uff09<\/p>\n<pre><code>String number = &quot;123&quot;;\nInteger result = Integer.valueOf(number);\n<\/code><\/pre>\n<h4>2\u3001Integer\u8f6cString<\/h4>\n<p>1\u3001String s = String.valueOf(i);<\/p>\n<ul>\n<li>\n<p>\u4f8b\u5b50<\/p>\n<\/li>\n<li>\n<pre><code>int a = 97;\nString s1 = String.valueOf(a);<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>2\u3001String s = Integer.toString(i);<\/p>\n<ul>\n<li>\n<p>\u4f8b\u5b50<\/p>\n<\/li>\n<li>\n<pre><code>int a = 97;\nString s2 = Integer.toString(a);<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>3\u3001String s = &quot;&quot; + i;<\/p>\n<ul>\n<li>\n<p>\u4f8b\u5b50<\/p>\n<\/li>\n<li>\n<pre><code>int a = 97;\nString s3 = \"\"+a;<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>43. \u5b57\u7b26\u4e32\u76f8\u4e58 \u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570 num1 \u548c num2\uff0c\u8fd4\u56de num1 \u548c num2  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-6040","post","type-post","status-publish","format-standard","hentry","category-63"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6040"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6040\/revisions"}],"predecessor-version":[{"id":6041,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6040\/revisions\/6041"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6040"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}