{"id":80262,"date":"2026-03-04T19:42:02","date_gmt":"2026-03-04T11:42:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/80262.html"},"modified":"2026-03-04T19:42:02","modified_gmt":"2026-03-04T11:42:02","slug":"leetcode-397-%e6%95%b4%e6%95%b0%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/80262.html","title":{"rendered":"LeetCode 397 \u6574\u6570\u66ff\u6362"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114200-69a81a8863a8e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114200-69a81a88ab909.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u6458\u8981<\/li>\n<li>\u63cf\u8ff0<\/li>\n<li>\u9898\u89e3\u7b54\u6848<\/li>\n<li>\u9898\u89e3\u4ee3\u7801\u5206\u6790<\/li>\n<li>\n<ul>\n<li>\u4e3a\u4ec0\u4e48\u5076\u6570\u76f4\u63a5\u9664\u4ee5 2<\/li>\n<li>\u5947\u6570\u65f6\u4e3a\u4ec0\u4e48\u770b n % 4<\/li>\n<li>\u4e3a\u4ec0\u4e48 n&#061;3 \u8981\u7279\u6b8a\u5904\u7406<\/li>\n<li>\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52<\/li>\n<li>\u793a\u4f8b 2 \u7b80\u8981\u8fc7\u7a0b&#xff1a;n &#061; 7<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c<\/li>\n<li>\n<ul>\n<li>\u793a\u4f8b 1&#xff1a;n &#061; 8<\/li>\n<li>\u793a\u4f8b 2&#xff1a;n &#061; 7<\/li>\n<li>\u793a\u4f8b 3&#xff1a;n &#061; 4<\/li>\n<li>\u793a\u4f8b 4&#xff1a;n &#061; 3<\/li>\n<li>\u793a\u4f8b 5&#xff1a;n &#061; 1<\/li>\n<\/ul>\n<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/li>\n<li>\u603b\u7ed3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u6458\u8981<\/h3>\n<p>\u8fd9\u9053\u9898\u7ed9\u4e00\u4e2a\u6b63\u6574\u6570 n&#xff0c;\u6bcf\u6b21\u53ef\u4ee5&#xff1a;\u82e5 n \u662f\u5076\u6570\u5219\u7528 n\/2 \u66ff\u6362&#xff1b;\u82e5 n \u662f\u5947\u6570\u5219\u7528 n&#043;1 \u6216 n-1 \u66ff\u6362\u3002\u95ee\u6700\u5c11\u51e0\u6b65\u80fd\u628a n \u53d8\u6210 1\u3002<\/p>\n<p>\u66b4\u529b\u53ef\u4ee5\u505a BFS \u6216\u8bb0\u5fc6\u5316\u641c\u7d22&#xff0c;\u4f46 n \u6700\u5927\u5230 2^31-1&#xff0c;\u72b6\u6001\u592a\u591a\u3002\u7528\u8d2a\u5fc3&#xff1a;\u5076\u6570\u4e00\u5b9a\u9664\u4ee5 2&#xff1b;\u5947\u6570\u65f6&#xff0c;\u5e0c\u671b\u4e0b\u4e00\u6b65\u80fd\u591a\u9664\u51e0\u6b21 2&#xff0c;\u6240\u4ee5\u770b n \u6a21 4\u2014\u2014\u82e5 n&#061;4k&#043;1 \u9009 n-1 \u5f97\u5230 2k \u80fd\u518d\u9664\u4e00\u6b21 2&#xff0c;\u82e5 n&#061;4k&#043;3 \u9009 n&#043;1 \u5f97\u5230 2k&#043;2 \u80fd\u518d\u9664\u4e00\u6b21 2\u3002\u7279\u4f8b\u662f n&#061;3&#xff1a;3\u21922\u21921 \u4e24\u6b65&#xff0c;3\u21924\u21922\u21921 \u4e09\u6b65&#xff0c;\u6240\u4ee5 3 \u8981\u9009 n-1\u3002\u6309\u8fd9\u4e2a\u89c4\u5219\u8fed\u4ee3\u6216\u9012\u5f52\u5373\u53ef\u5728 O(log n) \u5185\u5f97\u5230\u6700\u5c11\u6b65\u6570\u3002\u4e0b\u9762\u7528 Swift \u5b9e\u73b0\u5e76\u8bf4\u660e\u8d2a\u5fc3\u4f9d\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114200-69a81a88b60d9.png\" alt=\"\" \/><\/p>\n<h3>\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u5b9a\u6b63\u6574\u6570 n&#xff0c;\u53ef\u4ee5\u505a\u4e24\u79cd\u64cd\u4f5c&#xff1a;\u82e5 n \u4e3a\u5076\u6570&#xff0c;\u7528 n\/2 \u66ff\u6362 n&#xff1b;\u82e5 n \u4e3a\u5947\u6570&#xff0c;\u7528 n&#043;1 \u6216 n-1 \u66ff\u6362 n\u3002\u8fd4\u56de n \u53d8\u4e3a 1 \u6240\u9700\u7684\u6700\u5c0f\u66ff\u6362\u6b21\u6570\u3002<\/p>\n<p>\u793a\u4f8b 1&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a; n &#061; 8<br \/>\n\u8f93\u51fa&#xff1a; 3<br \/>\n\u89e3\u91ca&#xff1a; 8 -&gt; 4 -&gt; 2 -&gt; 1<\/p>\n<p>\u793a\u4f8b 2&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a; n &#061; 7<br \/>\n\u8f93\u51fa&#xff1a; 4<br \/>\n\u89e3\u91ca&#xff1a; 7 -&gt; 8 -&gt; 4 -&gt; 2 -&gt; 1 \u6216 7 -&gt; 6 -&gt; 3 -&gt; 2 -&gt; 1<\/p>\n<p>\u793a\u4f8b 3&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a; n &#061; 4<br \/>\n\u8f93\u51fa&#xff1a; 2<\/p>\n<p>\u63d0\u793a&#xff1a; 1 &lt;&#061; n &lt;&#061; 2^31 &#8211; 1<\/p>\n<p>\u6838\u5fc3\u601d\u8def&#xff1a;\u5076\u6570\u4e00\u5f8b\u9664\u4ee5 2&#xff1b;\u5947\u6570\u65f6\u7528\u300c\u4e0b\u4e00\u6b65\u80fd\u591a\u9664 2\u300d\u7684\u8d2a\u5fc3\u9009 &#043;1 \u6216 -1&#xff0c;\u5e76\u5355\u72ec\u5904\u7406 n&#061;3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114201-69a81a89057ed.png\" alt=\"\" \/><\/p>\n<h3>\u9898\u89e3\u7b54\u6848<\/h3>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">func<\/span> <span class=\"token function-definition function\">integerReplacement<\/span><span class=\"token punctuation\">(<\/span><span class=\"token omit keyword\">_<\/span> n<span class=\"token punctuation\">:<\/span> <span class=\"token class-name\">Int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token class-name\">Int<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">var<\/span> num <span class=\"token operator\">&#061;<\/span> n<br \/>\n        <span class=\"token keyword\">var<\/span> steps <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><br \/>\n        <span class=\"token keyword\">while<\/span> num <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> num <span class=\"token operator\">%<\/span> <span class=\"token number\">2<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                num <span class=\"token operator\">\/&#061;<\/span> <span class=\"token number\">2<\/span><br \/>\n                steps <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> num <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">3<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                    num <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><br \/>\n                    steps <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> num <span class=\"token operator\">%<\/span> <span class=\"token number\">4<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                    num <span class=\"token operator\">-&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n                    steps <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                    num <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n                    steps <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> steps<br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u9898\u89e3\u4ee3\u7801\u5206\u6790<\/h3>\n<h4>\u4e3a\u4ec0\u4e48\u5076\u6570\u76f4\u63a5\u9664\u4ee5 2<\/h4>\n<p>\u5076\u6570\u65f6\u53ea\u6709\u4e00\u79cd\u64cd\u4f5c&#xff1a;n\/2\u3002\u9664\u4ee5 2 \u80fd\u8ba9\u6570\u5c3d\u5feb\u53d8\u5c0f&#xff0c;\u4e14\u4e0d\u4f1a\u300c\u6d6a\u8d39\u300d\u4e00\u6b65\u53bb\u505a &#043;1\/-1 \u518d\u7b49\u4e0b\u6b21\u9664 2&#xff0c;\u6240\u4ee5\u5076\u6570\u65f6\u4e00\u5b9a\u9009 n\/2\u3002<\/p>\n<h4>\u5947\u6570\u65f6\u4e3a\u4ec0\u4e48\u770b n % 4<\/h4>\n<p>\u5947\u6570\u53ea\u80fd &#043;1 \u6216 -1&#xff0c;\u53d8\u6210\u5076\u6570\u540e\u518d\u9664 2\u3002\u6211\u4eec\u5e0c\u671b\u300c\u4e0b\u4e00\u6b65\u80fd\u8fde\u7eed\u9664 2 \u7684\u6b21\u6570\u5c3d\u91cf\u591a\u300d&#xff0c;\u8fd9\u6837\u66f4\u5feb\u63a5\u8fd1 1\u3002\u8bbe n&#061;2k&#043;1&#xff0c;\u5219 n-1&#061;2k\u3001n&#043;1&#061;2k&#043;2\u3002\u518d\u9664\u4e00\u6b21 2 \u5f97\u5230 k \u548c k&#043;1\u3002\u82e5 k \u662f\u5076\u6570&#xff0c;\u5219 (n-1)\/2 \u8fd8\u80fd\u518d\u9664 2&#xff1b;\u82e5 k&#043;1 \u662f\u5076\u6570&#xff0c;\u5219 (n&#043;1)\/2 \u8fd8\u80fd\u518d\u9664 2\u3002n&#061;4k&#043;1 \u65f6&#xff1a;(n-1)\/2&#061;2k \u4e3a\u5076&#xff0c;(n&#043;1)\/2&#061;2k&#043;1 \u4e3a\u5947&#xff0c;\u9009 n-1 \u66f4\u6709\u5229\u3002n&#061;4k&#043;3 \u65f6&#xff1a;(n-1)\/2&#061;2k&#043;1 \u4e3a\u5947&#xff0c;(n&#043;1)\/2&#061;2k&#043;2 \u4e3a\u5076&#xff0c;\u9009 n&#043;1 \u66f4\u6709\u5229\u3002\u6240\u4ee5\u89c4\u5219\u662f&#xff1a;n % 4 &#061;&#061; 1 \u7528 n-1&#xff0c;n % 4 &#061;&#061; 3 \u7528 n&#043;1\u3002<\/p>\n<h4>\u4e3a\u4ec0\u4e48 n&#061;3 \u8981\u7279\u6b8a\u5904\u7406<\/h4>\n<p>n&#061;3 \u65f6 3 % 4 &#061;&#061; 3&#xff0c;\u6309\u4e0a\u9762\u4f1a\u9009 n&#043;1 \u5f97\u5230 4&#xff0c;\u8def\u5f84 3\u21924\u21922\u21921 \u5171 3 \u6b65\u3002\u4f46\u82e5\u9009 n-1 \u5f97\u5230 2&#xff0c;\u8def\u5f84 3\u21922\u21921 \u53ea\u8981 2 \u6b65\u3002\u539f\u56e0\u662f 3 \u79bb 1 \u5df2\u7ecf\u5f88\u8fd1&#xff0c;&#043;1 \u53cd\u800c\u591a\u7ed5\u4e00\u6b65\u3002\u6240\u4ee5\u5355\u72ec\u5224\u65ad&#xff1a;\u82e5 n &#061;&#061; 3 \u5219\u7528 n-1\u3002<\/p>\n<h4>\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52<\/h4>\n<p>n \u6700\u5927 2^31-1&#xff0c;\u6309\u4e0a\u8ff0\u89c4\u5219\u6b65\u6570\u7ea6\u4e3a O(log n)&#xff0c;\u5faa\u73af\u8fed\u4ee3 log n \u6b21\u5373\u53ef&#xff0c;\u907f\u514d\u9012\u5f52\u6808\u3002\u7528 var num &#061; n \u548c while num !&#061; 1 \u6bcf\u6b21\u6309\u89c4\u5219\u66f4\u65b0 num \u5e76\u7d2f\u52a0 steps&#xff0c;\u6700\u540e\u8fd4\u56de steps\u3002<\/p>\n<h4>\u793a\u4f8b 2 \u7b80\u8981\u8fc7\u7a0b&#xff1a;n &#061; 7<\/h4>\n<p>7 \u4e3a\u5947\u4e14 7 % 4 &#061;&#061; 3&#xff0c;\u9009 &#043;1 \u2192 8&#xff1b;8 \u4e3a\u5076 \u2192 4 \u2192 2 \u2192 1\u3002\u5171 4 \u6b65&#xff0c;\u4e0e\u8f93\u51fa\u4e00\u81f4\u3002<\/p>\n<h3>\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c<\/h3>\n<h4>\u793a\u4f8b 1&#xff1a;n &#061; 8<\/h4>\n<p>8\u21924\u21922\u21921&#xff0c;3 \u6b65&#xff0c;\u8f93\u51fa 3\u3002<\/p>\n<h4>\u793a\u4f8b 2&#xff1a;n &#061; 7<\/h4>\n<p>7\u21928\u21924\u21922\u21921&#xff0c;4 \u6b65&#xff0c;\u8f93\u51fa 4\u3002<\/p>\n<h4>\u793a\u4f8b 3&#xff1a;n &#061; 4<\/h4>\n<p>4\u21922\u21921&#xff0c;2 \u6b65&#xff0c;\u8f93\u51fa 2\u3002<\/p>\n<h4>\u793a\u4f8b 4&#xff1a;n &#061; 3<\/h4>\n<p>3\u21922\u21921&#xff0c;2 \u6b65&#xff08;\u82e5\u6309 %4 \u9009 &#043;1 \u4f1a\u5f97\u5230 3 \u6b65&#xff0c;\u7279\u4f8b\u5904\u7406\u540e\u4e3a 2&#xff09;\u3002<\/p>\n<h4>\u793a\u4f8b 5&#xff1a;n &#061; 1<\/h4>\n<p>\u5df2\u662f 1&#xff0c;0 \u6b65&#xff0c;\u8f93\u51fa 0&#xff08;\u9898\u76ee n&gt;&#061;1&#xff0c;\u82e5\u4f20\u5165 1 \u5219\u5faa\u73af\u4e0d\u6267\u884c&#xff0c;\u8fd4\u56de 0&#xff09;\u3002<\/p>\n<h3>\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u6bcf\u6b21\u8981\u4e48\u9664\u4ee5 2&#xff08;n \u81f3\u5c11\u51cf\u534a&#xff09;&#xff0c;\u8981\u4e48 \u00b11 \u540e\u53d8\u6210\u5076\u6570\u518d\u9664&#xff0c;\u6574\u4f53\u89c4\u6a21\u7ea6\u6bcf\u6b21\u51cf\u534a\u6216\u63a5\u8fd1\u51cf\u534a&#xff0c;\u6b65\u6570 O(log n)&#xff0c;\u5355\u6b21\u64cd\u4f5c O(1)&#xff0c;\u603b\u65f6\u95f4 O(log n)\u3002<\/p>\n<h3>\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u53ea\u7528\u4e86\u82e5\u5e72\u53d8\u91cf&#xff0c;O(1)\u3002<\/p>\n<h3>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<p>\u7c7b\u4f3c\u300c\u7528\u6700\u5c11\u6b65\u6570\u628a\u6570\u53d8\u6210 1\u300d\u7684\u89c4\u5219\u5728\u6e38\u620f\u6b65\u6570\u3001\u72b6\u6001\u673a\u6700\u7b80\u8def\u5f84\u3001\u6216\u67d0\u4e9b\u7f16\u7801\/\u89e3\u7801\u6b65\u6570\u5206\u6790\u91cc\u4f1a\u78b0\u5230\u3002\u8d2a\u5fc3\u300c\u80fd\u9664 2 \u5c31\u9664\u3001\u5947\u6570\u65f6\u9009\u4e0b\u4e00\u6b65\u66f4\u80fd\u9664\u300d\u7684\u601d\u8def&#xff0c;\u5728\u89c4\u5219\u7b80\u5355\u3001\u6709\u660e\u786e\u300c\u66f4\u4f18\u4e0b\u4e00\u6b65\u300d\u65f6\u5e38\u7528\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u6574\u6570\u66ff\u6362\u7684\u6700\u5c0f\u6b65\u6570\u53ef\u4ee5\u7528\u8d2a\u5fc3&#xff1a;\u5076\u6570\u4e00\u5f8b n\/2&#xff1b;\u5947\u6570\u65f6 n % 4 &#061;&#061; 1 \u7528 n-1&#xff0c;n % 4 &#061;&#061; 3 \u7528 n&#043;1&#xff0c;\u5e76\u5355\u72ec\u628a n&#061;3 \u8bbe\u4e3a n-1\u3002\u6309\u6b64\u89c4\u5219\u8fed\u4ee3\u76f4\u5230 n \u53d8\u4e3a 1&#xff0c;\u6b65\u6570\u5373\u4e3a\u7b54\u6848&#xff0c;\u65f6\u95f4 O(log n)\u3001\u7a7a\u95f4 O(1)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u76ee\u5f55\u6458\u8981\u63cf\u8ff0\u9898\u89e3\u7b54\u6848\u9898\u89e3\u4ee3\u7801\u5206\u6790\u4e3a\u4ec0\u4e48\u5076\u6570\u76f4\u63a5\u9664\u4ee5 2\u5947\u6570\u65f6\u4e3a\u4ec0\u4e48\u770b n % 4\u4e3a\u4ec0\u4e48 n3 \u8981\u7279\u6b8a\u5904\u7406\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52\u793a\u4f8b 2 \u7b80\u8981\u8fc7\u7a0b&#xff1a;n  7\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c\u793a\u4f8b 1&#xff1a;n  8\u793a\u4f8b 2&#xff1a;n  7\u793a\u4f8b 3&#xff1a;n  4\u793a\u4f8b 4&#xff1a;n  3\u793a\u4f8b 5&#xff1a;n  1\u65f6\u95f4\u590d\u6742\u5ea6\u7a7a\u95f4\u590d\u6742\u5ea6\u5b9e\u9645\u5e94\u7528\u573a\u666f\u603b\u7ed3\u6458\u8981<br \/>\n\u8fd9\u9053\u9898\u7ed9\u4e00\u4e2a\u6b63\u6574\u6570 n&#xff0c;\u6bcf\u6b21\u53ef\u4ee5&#xff1a;\u82e5 n \u662f\u5076\u6570\u5219\u7528 n\/2 \u66ff\u6362&amp;<\/p>\n","protected":false},"author":2,"featured_media":80258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2692,427,925],"topic":[],"class_list":["post-80262","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-leetcode","tag-427","tag-925"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/80262.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u76ee\u5f55\u6458\u8981\u63cf\u8ff0\u9898\u89e3\u7b54\u6848\u9898\u89e3\u4ee3\u7801\u5206\u6790\u4e3a\u4ec0\u4e48\u5076\u6570\u76f4\u63a5\u9664\u4ee5 2\u5947\u6570\u65f6\u4e3a\u4ec0\u4e48\u770b n % 4\u4e3a\u4ec0\u4e48 n3 \u8981\u7279\u6b8a\u5904\u7406\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52\u793a\u4f8b 2 \u7b80\u8981\u8fc7\u7a0b&#xff1a;n 7\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c\u793a\u4f8b 1&#xff1a;n 8\u793a\u4f8b 2&#xff1a;n 7\u793a\u4f8b 3&#xff1a;n 4\u793a\u4f8b 4&#xff1a;n 3\u793a\u4f8b 5&#xff1a;n 1\u65f6\u95f4\u590d\u6742\u5ea6\u7a7a\u95f4\u590d\u6742\u5ea6\u5b9e\u9645\u5e94\u7528\u573a\u666f\u603b\u7ed3\u6458\u8981 \u8fd9\u9053\u9898\u7ed9\u4e00\u4e2a\u6b63\u6574\u6570 n&#xff0c;\u6bcf\u6b21\u53ef\u4ee5&#xff1a;\u82e5 n \u662f\u5076\u6570\u5219\u7528 n\/2 \u66ff\u6362&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/80262.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-04T11:42:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114200-69a81a8863a8e.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80262.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/80262.html\",\"name\":\"LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-04T11:42:02+00:00\",\"dateModified\":\"2026-03-04T11:42:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/80262.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/80262.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80262.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LeetCode 397 \u6574\u6570\u66ff\u6362\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/80262.html","og_locale":"zh_CN","og_type":"article","og_title":"LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u76ee\u5f55\u6458\u8981\u63cf\u8ff0\u9898\u89e3\u7b54\u6848\u9898\u89e3\u4ee3\u7801\u5206\u6790\u4e3a\u4ec0\u4e48\u5076\u6570\u76f4\u63a5\u9664\u4ee5 2\u5947\u6570\u65f6\u4e3a\u4ec0\u4e48\u770b n % 4\u4e3a\u4ec0\u4e48 n3 \u8981\u7279\u6b8a\u5904\u7406\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52\u793a\u4f8b 2 \u7b80\u8981\u8fc7\u7a0b&#xff1a;n 7\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c\u793a\u4f8b 1&#xff1a;n 8\u793a\u4f8b 2&#xff1a;n 7\u793a\u4f8b 3&#xff1a;n 4\u793a\u4f8b 4&#xff1a;n 3\u793a\u4f8b 5&#xff1a;n 1\u65f6\u95f4\u590d\u6742\u5ea6\u7a7a\u95f4\u590d\u6742\u5ea6\u5b9e\u9645\u5e94\u7528\u573a\u666f\u603b\u7ed3\u6458\u8981 \u8fd9\u9053\u9898\u7ed9\u4e00\u4e2a\u6b63\u6574\u6570 n&#xff0c;\u6bcf\u6b21\u53ef\u4ee5&#xff1a;\u82e5 n \u662f\u5076\u6570\u5219\u7528 n\/2 \u66ff\u6362&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/80262.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-04T11:42:02+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260304114200-69a81a8863a8e.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/80262.html","url":"https:\/\/www.wsisp.com\/helps\/80262.html","name":"LeetCode 397 \u6574\u6570\u66ff\u6362 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-04T11:42:02+00:00","dateModified":"2026-03-04T11:42:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/80262.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/80262.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/80262.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LeetCode 397 \u6574\u6570\u66ff\u6362"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=80262"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80262\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/80258"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=80262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=80262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=80262"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=80262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}