{"id":49207,"date":"2024-12-02T15:45:55","date_gmt":"2024-12-02T07:45:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49207\/"},"modified":"2024-12-02T15:45:55","modified_gmt":"2024-12-02T07:45:55","slug":"%e8%bd%ac%e6%8d%a2%e5%90%8e%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%95%b0%e5%ad%97%e6%80%bb%e5%92%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49207\/","title":{"rendered":"\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/1731823852673988ec477f4.jpg\" class=\"aligncenter\" title=\"\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c\u63d2\u56fe\" alt=\"\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c\u63d2\u56fe\" \/><\/p>\n<p>1945 \u5e74\u3002\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c<\/p>\n<p><strong>\u96be\u5ea6\uff1a<\/strong>\u7b80\u5355<\/p>\n<p><strong>\u4e3b\u9898\uff1a<\/strong>\u5b57\u7b26\u4e32\u3001\u6a21\u62df<\/p>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 s \u548c\u4e00\u4e2a\u6574\u6570 k\u3002<\/p>\n<p>\u9996\u5148\uff0c<strong>\u5c06<\/strong> s \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u65b9\u6cd5\u662f\u5c06\u6bcf\u4e2a\u5b57\u6bcd\u66ff\u6362\u4e3a\u5176\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\uff08\u5373\uff0c\u5c06 &#8216;a&#8217; \u66ff\u6362\u4e3a 1\uff0c\u5c06 &#8216;b&#8217; \u66ff\u6362\u4e3a 2\uff0c&#8230;\uff0c\u5c06 &#8216;z&#8217; \u66ff\u6362\u4e3a26\uff09\u3002\u7136\u540e\uff0c<strong>\u8f6c\u6362<\/strong>\u6574\u6570\uff0c\u5c06\u5176\u66ff\u6362\u4e3a<strong>\u5176\u6570\u5b57\u4e4b\u548c<\/strong>\u3002\u603b\u5171\u91cd\u590d<strong>\u53d8\u6362<\/strong>\u64cd\u4f5ck <strong>\u6b21<\/strong>\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c s = &#8220;zbax&#8221; \u4e14 k = 2\uff0c\u5219\u901a\u8fc7\u4ee5\u4e0b\u64cd\u4f5c\u5f97\u5230\u7684\u6574\u6570\u5c06\u4e3a 8\uff1a<\/p>\n<ul>\n<li> <strong>\u8f6c\u6362\uff1a<\/strong>\u201czbax\u201d\u201c(26)(2)(1)(24)\u201d\u201c262124\u201d262124 <\/li>\n<li> <strong>\u53d8\u6362 #1\uff1a<\/strong> 262124 2 + 6 + 2 + 1 + 2 + 4 17 <\/li>\n<li> <strong>\u53d8\u6362 #2\uff1a<\/strong> 17 1 + 7 8 <\/li>\n<\/ul>\n<p>\u8fd4\u56de<em>\u6267\u884c\u4e0a\u8ff0\u64cd\u4f5c\u540e\u5f97\u5230\u7684\u6574\u6570<\/em>\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> s = &#8220;iiii&#8221;, k = 1<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong> 36<\/li>\n<li> <strong>\u8bf4\u660e\uff1a<\/strong>\u64cd\u4f5c\u5982\u4e0b\uff1a\n<ul>\n<li>\u8f6c\u6362\uff1a\u201ciiii\u201d\u201c(9)(9)(9)(9)\u201d\u201c9999\u201d9999<\/li>\n<li>\u53d8\u6362 #1\uff1a9999 9 + 9 + 9 + 9 36<\/li>\n<li>\u56e0\u6b64\u7ed3\u679c\u6574\u6570\u662f 36\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> s = &#8220;leetcode&#8221;, k = 2<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong> 6<\/li>\n<li> <strong>\u8bf4\u660e\uff1a<\/strong>\u64cd\u4f5c\u5982\u4e0b\uff1a\n<ul>\n<li>\u8f6c\u6362\uff1a\u201cleetcode\u201d\u201c(12)(5)(5)(20)(3)(15)(4)(5)\u201d\u201c12552031545\u201d12552031545<\/li>\n<li>\u53d8\u6362 #1\uff1a12552031545 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 33<\/li>\n<li>\u53d8\u6362 #2\uff1a33 3 + 3 6<\/li>\n<li>\u56e0\u6b64\u7ed3\u679c\u6574\u6570\u662f 6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> s = &#8220;zbax&#8221;, k = 2<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong> 8<\/li>\n<\/ul>\n<p><strong>\u7ea6\u675f\uff1a<\/strong><\/p>\n<ul>\n<li>1 &lt;= s.length &lt;= 100<\/li>\n<li>1 &lt;= k &lt;= 10<\/li>\n<li> s \u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u6211\u4eec\u8981\u6ce8\u610f\uff0c\u7b2c\u4e00\u6b21\u53d8\u6362\u540e\u7684\u503c\u6700\u591a\u4e3a 100 * 10\uff0c\u8fd9\u4e2a\u503c\u5e76\u4e0d\u591a<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53d8\u6362\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u66b4\u529b\u6765\u5b8c\u6210\u5176\u4f59\u7684\u53d8\u6362<\/li>\n<\/ol>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u89e3\u51b3\u65b9\u6848\u5206\u4e3a\u4e24\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u5c06\u5b57\u7b26\u4e32 s \u8f6c\u6362\u4e3a\u6574\u6570<\/strong>:<\/p>\n<ul>\n<li>\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f1a\u66ff\u6362\u4e3a\u5b57\u6bcd\u8868\u4e2d\u76f8\u5e94\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\uff0c&#8217;a&#8217; -&gt; 1\u3001&#8217;b&#8217; -&gt; 2\u3001&#8230;\u3001&#8217;z&#8217; -&gt; 26\uff09\u3002<\/li>\n<li>\u5c06\u6240\u6709\u8fd9\u4e9b\u6570\u5b57\u8fde\u63a5\u8d77\u6765\u5f62\u6210\u4e00\u4e2a\u5927\u6574\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u5c06\u6574\u6570\u7684\u6570\u5b57\u76f8\u52a0 k \u6b21\u6765\u8f6c\u6362\u6574\u6570<\/strong>:<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u53d8\u6362\uff0c\u5c06\u5f53\u524d\u6570\u5b57\u7684\u6240\u6709\u6570\u5b57\u76f8\u52a0\u3002<\/li>\n<li>\u91cd\u590d\u6b64\u8f6c\u6362\u8fc7\u7a0b k \u6b21\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u8ba9\u6211\u4eec\u7528 php \u5b9e\u73b0\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a<strong>1945\u3002\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c<\/strong><\/p>\n<pre>&lt;?php\nfunction getLucky($s, $k) {\n    ...\n    ...\n    ...\n    \/**\n     * go to .\/solution.php\n     *\/\n}\n\n\/\/ Test cases\necho getLucky(\"iiii\", 1) . \"\\n\"; \/\/ Output: 36\necho getLucky(\"leetcode\", 2) . \"\\n\"; \/\/ Output: 6\necho getLucky(\"zbax\", 2) . \"\\n\"; \/\/ Output: 8\n?&gt;\n<\/pre>\n<h3> \u89e3\u91ca\uff1a <\/h3>\n<ol>\n<li>\n<p><strong>\u8f6c\u6362\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<ul>\n<li>\u6211\u4eec\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32 s \u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528 ord($s[$i]) &#8211; ord(&#8216;a&#8217;) + 1 \u8ba1\u7b97\u5176\u5bf9\u5e94\u7684\u5b57\u6bcd\u4f4d\u7f6e\u3002<\/li>\n<li>\u8fd9\u4e9b\u503c\u8fde\u63a5\u8d77\u6765\u5f62\u6210\u4e00\u4e2a\u4ee3\u8868\u6570\u5b57\u7684\u5927\u5b57\u7b26\u4e32 numstr\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362\u6570\u5b57\uff1a<\/strong><\/p>\n<ul>\n<li>\u6211\u4eec\u5faa\u73af k \u6b21\uff0c\u6bcf\u6b21\u5bf9\u5f53\u524d numstr \u7684\u6570\u5b57\u6c42\u548c\u3002<\/li>\n<li>\u6b64\u6c42\u548c\u8fd0\u7b97\u7684\u7ed3\u679c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\u56de numstr \u4e2d\uff0c\u4ee5\u5141\u8bb8\u8fdb\u4e00\u6b65\u8f6c\u6362\u3002<\/li>\n<li>\u7ecf\u8fc7 k \u6b21\u53d8\u6362\uff0c\u6211\u4eec\u8fd4\u56de\u6700\u7ec8\u7684\u6574\u6570\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3> \u6d4b\u8bd5\u7528\u4f8b\uff1a <\/h3>\n<ul>\n<li> k = 1 \u7684\u201ciiii\u201d\u8f6c\u6362\u4e3a\u201c9999\u201d\uff0c\u603b\u548c\u4e3a 36\uff0c\u7531\u4e8e k=1\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a 36\u3002<\/li>\n<li> k = 2 \u7684\u201cleetcode\u201d\u8f6c\u6362\u4e3a\u201c12552031545\u201d\uff0c\u5728\u7b2c\u4e00\u6b21\u8f6c\u6362\u4e2d\u603b\u548c\u4e3a 33\uff0c\u5728\u7b2c\u4e8c\u6b21\u8f6c\u6362\u4e2d\u4e3a 6\uff0c\u7ed3\u679c\u4e3a 6\u3002<\/li>\n<li> k = 2 \u7684\u201czbax\u201d\u8f6c\u6362\u4e3a\u201c262124\u201d\uff0c\u5728\u7b2c\u4e00\u6b21\u8f6c\u6362\u4e2d\u603b\u8ba1\u4e3a 17\uff0c\u5728\u7b2c\u4e8c\u6b21\u8f6c\u6362\u4e2d\u603b\u8ba1\u4e3a 8\uff0c\u7ed3\u679c\u4e3a 8\u3002<\/li>\n<\/ul>\n<p>\u8003\u8651\u5230\u9650\u5236\uff0c\u8be5\u89e3\u51b3\u65b9\u6848\u975e\u5e38\u6709\u6548\uff0c\u5e76\u4e14\u5728\u63d0\u4f9b\u7684\u9650\u5236\u5185\u53ef\u4ee5\u5f88\u597d\u5730\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u8054\u7cfb\u94fe\u63a5<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u53d1\u73b0\u672c\u7cfb\u5217\u6709\u5e2e\u52a9\uff0c\u8bf7\u8003\u8651\u5728 github \u4e0a\u7ed9<strong>\u5b58\u50a8\u5e93<\/strong> \u4e00\u4e2a\u661f\u661f\uff0c\u6216\u5728\u60a8\u6700\u559c\u6b22\u7684\u793e\u4ea4\u7f51\u7edc\u4e0a\u5206\u4eab\u8be5\u5e16\u5b50?\u3002\u60a8\u7684\u652f\u6301\u5bf9\u6211\u6765\u8bf4\u610f\u4e49\u91cd\u5927\uff01<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u8981\u66f4\u591a\u7c7b\u4f3c\u7684\u6709\u7528\u5185\u5bb9\uff0c\u8bf7\u968f\u65f6\u5173\u6ce8\u6211\uff1a<\/p>\n<ul>\n<li><strong>\u9886\u82f1<\/strong><\/li>\n<li><strong>github<\/strong><\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 1945 \u5e74\u3002\u8f6c\u6362\u540e\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u603b\u548c \u96be\u5ea6\uff1a\u7b80\u5355 \u4e3b\u9898\uff1a\u5b57\u7b26\u4e32\u3001\u6a21\u62df \u7ed9\u4f60\u4e00\u4e2a\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 s \u548c\u4e00\u4e2a\u6574\u6570 k\u3002 \u9996\u5148\uff0c\u5c06 s \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u65b9\u6cd5\u662f\u5c06\u6bcf\u4e2a\u5b57\u6bcd\u66ff\u6362\u4e3a\u5176\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\uff08\u5373\uff0c\u5c06 &#8216;a&#8217; \u66ff\u6362\u4e3a 1\uff0c\u5c06 &#8216;b&#8217; \u66ff\u6362\u4e3a 2\uff0c&#8230;\uff0c\u5c06 &#8216;z&#8217; \u66ff\u6362\u4e3a26\uff09\u3002\u7136\u540e\uff0c\u8f6c\u6362\u6574\u6570\uff0c\u5c06\u5176\u66ff\u6362\u4e3a\u5176\u6570\u5b57\u4e4b\u548c\u3002\u603b\u5171\u91cd\u590d\u53d8\u6362\u64cd\u4f5ck \u6b21\u3002 \u4f8b\u5982\uff0c\u5982\u679c s = &#8220;zbax&#8221; \u4e14 k = 2\uff0c\u5219\u901a\u8fc7\u4ee5\u4e0b\u64cd\u4f5c\u5f97\u5230\u7684\u6574\u6570\u5c06\u4e3a 8\uff1a \u8f6c\u6362\uff1a\u201czbax\u201d\u201c(26)(2)(1)(24)\u201d\u201c262124\u201d262124 \u53d8\u6362 #1\uff1a 262124 2 + 6 + 2 + 1 + 2 + 4 17 \u53d8\u6362 #2\uff1a 17 1 + 7 8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49207","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49207"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49207\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}