{"id":1232,"date":"2024-05-10T09:14:00","date_gmt":"2024-05-10T01:14:00","guid":{"rendered":"http:\/\/www.jusesgod.com\/?p=1232"},"modified":"2024-05-10T09:32:21","modified_gmt":"2024-05-10T01:32:21","slug":"nc-001-%e5%a4%a7%e6%95%b0%e5%8a%a0%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.jusesgod.com\/?p=1232","title":{"rendered":"NC 001 \u5927\u6570\u52a0\u6cd5"},"content":{"rendered":"\n<p>\u9898\u76ee\uff1a<br>\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8bfb\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\uff0c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\u3002<br>\u6570\u636e\u8303\u56f4\uff1a<img decoding=\"async\" src=\"https:\/\/www.nowcoder.com\/equation?tex=s.length%2Ct.length%20%5Cle%20100000\">\uff0c\u5b57\u7b26\u4e32\u4ec5\u7531&#8217;0&#8217;~\u20189\u2019\u6784\u6210<br>\u8981\u6c42\uff1a\u65f6\u95f4\u590d\u6742\u5ea6&nbsp;<img decoding=\"async\" src=\"https:\/\/www.nowcoder.com\/equation?tex=O(n)\"><\/p>\n\n\n\n<p>\u9996\u5148\u5148\u60f3\u5230\u7684\u8fd8\u662f\u901a\u8fc7\u4ece\u53f3\u5411\u5de6\u904d\u5386\u4e00\u4f4d\u4e00\u4f4d\u76f8\u52a0\uff0c\u50cf\u662f\u5c0f\u5b66\u751f\u624b\u7b97\u4e00\u6837\u8ba1\u7b97\u8fdb\u4f4d\u6700\u540e\u5f97\u5230\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\n\npublic class Solution {\n    \/**\n     * \u4ee3\u7801\u4e2d\u7684\u7c7b\u540d\u3001\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u540d\u5df2\u7ecf\u6307\u5b9a\uff0c\u8bf7\u52ff\u4fee\u6539\uff0c\u76f4\u63a5\u8fd4\u56de\u65b9\u6cd5\u89c4\u5b9a\u7684\u503c\u5373\u53ef\n     *\n     * \u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\n     * @param s string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e00\u4e2a\u6574\u6570\n     * @param t string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e8c\u4e2a\u6574\u6570\n     * @return string\u5b57\u7b26\u4e32\n     *\/\n    public String solve (String s, String t) {\n        \/\/ write code here\n        \/\/\u6ea2\u51fa\u6807\u8bb0\n        int over = 0;\n        String result = \"\";\n        int index = 1;\n        int loopleft = Math.max(s.length(), t.length());\n        int&#91;] s_array = s.chars().toArray();\n        int&#91;] t_array = t.chars().toArray();\n        for(; loopleft > 0; loopleft--,index ++){\n            int a = s.length() >= index ? (s_array&#91;s.length() - index] - '0'):0;\n            int b = t.length() >= index ? (t_array&#91;t.length() - index] - '0'):0;\n            result = ((a+b+over)>0 ? (a+b+over) % 10 : 0) + result;\n            over = (a+b+over)>0 ? (a+b+over) \/ 10 : 0; \n        }\n        result = (over==0 ? \"\" : \"1\") + result; \n        \n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-52f67d1912aebeb72c1924bb756aad72\">\u8fd0\u884c\u65f6\u95f4\uff1a 2001 ms&nbsp;\u5360\u7528\u5185\u5b58\uff1a9912K&nbsp;\u72b6\u6001\uff1a\u8fd0\u884c\u8d85\u65f6<\/p>\n\n\n\n<p>\u8d85\u4e86\u8fd1\u4e00\u500d\u7684\u65f6\u95f4\uff0c\u663e\u7136\u4e0d\u884c\uff0c\u90a3\u4e48\u5c31\u8981\u51cf\u5c11\u8fd0\u7b97\u6b21\u6570\uff0c\u6211\u5148\u60f3\u5230\u7684\u662f\u8f6c\u6362\u51fd\u6570paresInt\u548ccharAt\uff0c\u867d\u7136\u662fjava\u5185\u90e8\u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u56e0\u4e3a\u662f\u901a\u7528\u65b9\u6cd5\uff0c\u4f1a\u6709\u5f88\u591a\u5197\u4f59\u5224\u65ad\uff0c\u6bd4\u5982\u4e0b\u6807\u957f\u5ea6\u4e4b\u7c7b\u7684\uff0c\u56e0\u6b64\u6211\u9009\u62e9\u7528switch\u76f4\u63a5\u6bd4\u8f83\u6765\u6620\u5c04\u7ed3\u679c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.util.*;\n\n\npublic class Solution {\n    \/**\n     * \u4ee3\u7801\u4e2d\u7684\u7c7b\u540d\u3001\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u540d\u5df2\u7ecf\u6307\u5b9a\uff0c\u8bf7\u52ff\u4fee\u6539\uff0c\u76f4\u63a5\u8fd4\u56de\u65b9\u6cd5\u89c4\u5b9a\u7684\u503c\u5373\u53ef\n     *\n     * \u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\n     * @param s string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e00\u4e2a\u6574\u6570\n     * @param t string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e8c\u4e2a\u6574\u6570\n     * @return string\u5b57\u7b26\u4e32\n     *\/\n    public String solve (String s, String t) {\n        \/\/ write code here\n        \/\/\u6ea2\u51fa\u6807\u8bb0\n        int over = 0;\n        long start = System.currentTimeMillis();\n        int SPLIT_LENGTH = 16;\n        String result = &quot;&quot;, format = &quot;&quot;;\n        for(int i = 0; i&lt; SPLIT_LENGTH; i++){\n            format += &quot;0&quot;;\n        }\n        long a,b;\n        int times = Math.max((s.length()-1)\/SPLIT_LENGTH, (t.length()-1)\/SPLIT_LENGTH);\n        int index = 0, sl = s.length(), tl = t.length();\n        boolean sOut = false, tOut = false;\n        int s_start, s_end, t_start, t_end;\n        for(;times &gt;= 0; times--,index++){\n            if(sOut){\n                a = 0l;\n            }else if((index+1)*SPLIT_LENGTH &gt;= sl-1){\n                System.out.println(&quot;s is out&quot;);\n                s_start = 0;\n                if(index == 0){\n                    s_end = sl;\n                }else{\n                    s_end = sl - index * SPLIT_LENGTH;\n                }\n                sOut = true;\n                a = Long.parseLong(s.substring(s_start, s_end));\n            }else{\n                s_start = sl - (index+1)*SPLIT_LENGTH;\n                s_end = sl - index * SPLIT_LENGTH;\n                a = Long.parseLong(s.substring(s_start, s_end));\n            }\n           \n            if(tOut){\n                b = 0l;\n            }else if((index+1)*SPLIT_LENGTH &gt;= tl-1){\n                System.out.println(&quot;t is out&quot;);\n                t_start = 0;\n                if(index == 0){\n                    t_end = tl;\n                }else{\n                    t_end = tl - index * SPLIT_LENGTH;\n                }\n                tOut = true;\n                b = Long.parseLong(t.substring(t_start, t_end));\n            }else{\n                t_start = tl - (index+1)*SPLIT_LENGTH;\n                t_end = tl - index * SPLIT_LENGTH;\n                b = Long.parseLong(t.substring(t_start, t_end));\n            }\n\n            System.out.println(&quot;a:&quot; + a + &quot; b:&quot; + b + &quot; over:&quot; + over);\n            Long tmp = a + b + over;\n            \n            if( sOut &amp;&amp; tOut){\n                result = tmp + result;\n            }else{\n                if(tmp.toString().length() &gt; SPLIT_LENGTH) over = 1; else over = 0;\n                result = (format + tmp).substring((&quot;&quot;+tmp).length()) + result;\n            }\n        }\n        return result;\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u8fd9\u6b21\u4fee\u6539\u901a\u8fc7long\u5f62\u8ba1\u7b97\u6765\u63d0\u9ad8\u7b97\u901f\uff0c\u7528\u4e00\u4e2aSPLIT_LENGTH\u6765\u63a7\u5236\u5206\u7ec4\u5927\u5c0f\uff0c\u6700\u7ec8\u8fbe\u5230358ms\u901a\u8fc7\u4e86\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u67e5\u770b\u6392\u540d\u53d1\u73b0\uff0c\u6700\u5feb\u7684\u53ea\u9700\u898153ms\uff0c\u4f7f\u7528\u7684\u8fd8\u662f\u7b2c\u4e00\u79cd\u601d\u8def\uff0c\u4e8e\u662f\u5c1d\u8bd5\u4f18\u5316\u7b2c\u4e00\u79cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\n\npublic class Solution {\n    \/**\n     * \u4ee3\u7801\u4e2d\u7684\u7c7b\u540d\u3001\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u540d\u5df2\u7ecf\u6307\u5b9a\uff0c\u8bf7\u52ff\u4fee\u6539\uff0c\u76f4\u63a5\u8fd4\u56de\u65b9\u6cd5\u89c4\u5b9a\u7684\u503c\u5373\u53ef\n     *\n     * \u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\n     * @param s string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e00\u4e2a\u6574\u6570\n     * @param t string\u5b57\u7b26\u4e32 \u8868\u793a\u7b2c\u4e8c\u4e2a\u6574\u6570\n     * @return string\u5b57\u7b26\u4e32\n     *\/\n    public String solve (String s, String t) {\n        \/\/ write code here\n        \/\/\u6ea2\u51fa\u6807\u8bb0\n        int over = 0;\n        int index = 1, sl=s.length(), tl=t.length();\n        char&#91;] result = new char&#91;Math.max(sl, tl)];\n        for(int loopleft = Math.max(sl, tl); loopleft &gt;= index; index++){\n            if(sl &gt;= index) over += s.charAt(sl - index)-'0';\n            if(tl &gt;= index) over += t.charAt(tl - index)-'0';\n            result&#91;loopleft - index] = (char)('0' + over % 10);\n            over = over \/ 10;\n        }\n        return (over==0?\"\":\"1\") + new String(result);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec851ms\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\uff1a\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8bfb\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u8ba1\u7b97\u5b83&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1232","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1232"}],"version-history":[{"count":4,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions"}],"predecessor-version":[{"id":1238,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions\/1238"}],"wp:attachment":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}