{"id":64559,"date":"2026-01-23T19:04:23","date_gmt":"2026-01-23T11:04:23","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/64559.html"},"modified":"2026-01-23T19:04:23","modified_gmt":"2026-01-23T11:04:23","slug":"leetcode%e5%9b%9e%e6%ba%af%e7%ae%97%e6%b3%95%ef%bc%88131-%e5%88%86%e5%89%b2%e5%9b%9e%e6%96%87%e4%b8%b2%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/64559.html","title":{"rendered":"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"635\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260123110422-697355b621019.png\" width=\"836\" \/><\/p>\n<\/p>\n<p>bool isPalindrome(string &amp;s, int start, int end)  \/\/ \u2705 \u5f15\u7528\u4f20\u9012 <\/p>\n<p>\u56e0\u4e3a&#xff1a;<\/p>\n<li>\n<p>isPalindrome\u00a0\u53ea\u662f\u8bfb\u53d6\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u4fee\u6539\u5b83<\/p>\n<\/li>\n<li>\n<p>\u4f46\u6700\u597d\u52a0\u4e0a\u00a0const&#xff1a;bool isPalindrome(const string &amp;s, &#8230;)<\/p>\n<\/li>\n<p>\/\/ \u4f7f\u7528 const &amp;&#xff0c;\u56e0\u4e3a\u4e0d\u4fee\u6539\u5b57\u7b26\u4e32&#xff0c;\u53ea\u8bfb\u53d6<br \/>\nvoid backtracking(const string &amp;s, int startIndex) <\/p>\n<p>\u4e3a\u4ec0\u4e48 partition \u51fd\u6570\u4e2d\u7684 s \u4e0d\u7528 &amp;&#xff1f;<\/p>\n<p>vector&lt;vector&lt;string&gt;&gt; partition(string s)  \/\/ \u8fd9\u91cc\u4e0d\u7528 &amp; <\/p>\n<p>\u8c03\u7528\u8005\u4f20\u53c2\u7075\u6d3b&#xff1a;\u5982\u679c\u8fd9\u91cc\u7528\u00a0string &amp;s&#xff0c;\u8c03\u7528\u8005\u5fc5\u987b\u4f20\u9012\u4e00\u4e2a\u5df2\u6709\u7684 string \u53d8\u91cf<\/p>\n<p>string str &#061; &#034;aab&#034;;<br \/>\npartition(str);  \/\/ \u9700\u8981\u5df2\u6709\u53d8\u91cf <\/p>\n<p>\u53ef\u4ee5\u63a5\u53d7\u4e34\u65f6\u5bf9\u8c61&#xff1a;\u4e0d\u7528 &amp; &#xff0c;\u5373\u53ef\u4ee5\u63a5\u53d7\u4e34\u65f6\u521b\u5efa\u7684\u5b57\u7b26\u4e32<\/p>\n<p>partition(&#034;aab&#034;);  \/\/ \u4e34\u65f6\u5b57\u7b26\u4e32<br \/>\npartition(string(&#034;aab&#034;));  \/\/ \u4e34\u65f6\u5bf9\u8c61 <\/p>\n<p>string str &#061; s.substr(startIndex, i &#8211; startIndex &#043; 1); <\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>\n<p>startIndex&#xff1a;\u5b50\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff08;\u4ece0\u5f00\u59cb\u8ba1\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>i &#8211; startIndex &#043; 1&#xff1a;\u8981\u63d0\u53d6\u7684\u5b57\u7b26\u6570\u91cf&#xff08;\u5b50\u4e32\u7684\u957f\u5ea6&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>s.substr(startIndex);  \/\/ \u63d0\u53d6\u4ecestartIndex\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u6240\u6709\u5b57\u7b26<br \/>\ns.substr();           \/\/ \u63d0\u53d6\u6574\u4e2a\u5b57\u7b26\u4e32 <\/p>\n<p>\u4ece\u6811\u5f62\u7ed3\u6784\u7684\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa&#xff1a;\u5207\u5272\u7ebf\u5207\u5230\u4e86\u5b57\u7b26\u4e32\u6700\u540e\u9762&#xff0c;\u8bf4\u660e\u627e\u5230\u4e86\u4e00\u79cd\u5207\u5272\u65b9\u6cd5&#xff0c;\u6b64\u65f6\u5c31\u662f\u672c\u5c42\u9012\u5f52\u7684\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n<p>\u5728\u5904\u7406\u7ec4\u5408\u95ee\u9898\u7684\u65f6\u5019&#xff0c;\u9012\u5f52\u53c2\u6570\u9700\u8981\u4f20\u5165startIndex&#xff0c;\u8868\u793a\u4e0b\u4e00\u8f6e\u9012\u5f52\u904d\u5386\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8fd9\u4e2astartIndex\u5c31\u662f\u5207\u5272\u7ebf\u3002<\/p>\n<p>\u5728\u9012\u5f52\u5faa\u73af\u4e2d\u5982\u4f55\u622a\u53d6\u5b50\u4e32\u5462&#xff1f;<\/p>\n<p>\u5728for (int i &#061; startIndex; i &lt; s.size(); i&#043;&#043;)\u5faa\u73af\u4e2d&#xff0c;\u6211\u4eec \u5b9a\u4e49\u4e86\u8d77\u59cb\u4f4d\u7f6estartIndex&#xff0c;\u90a3\u4e48 [startIndex, i] \u5c31\u662f\u8981\u622a\u53d6\u7684\u5b50\u4e32\u3002<\/p>\n<p>\u6240\u4ee5\u5b50\u4e32\u7684\u957f\u5ea6\u4e3a i &#8211; startIndex&#043;1<\/p>\n<p>\u9996\u5148\u5224\u65ad\u8fd9\u4e2a\u5b50\u4e32\u662f\u4e0d\u662f\u56de\u6587&#xff0c;\u5982\u679c\u662f\u56de\u6587&#xff0c;\u5c31\u52a0\u5165\u5728vector&lt;string&gt; path\u4e2d&#xff0c;path\u7528\u6765\u8bb0\u5f55\u5207\u5272\u8fc7\u7684\u56de\u6587\u5b50\u4e32\u3002<\/p>\n<p>class Solution {<br \/>\nprivate:<br \/>\n    vector&lt;vector&lt;string&gt;&gt; result; \/\/ \u5b58\u50a8\u6240\u6709\u53ef\u80fd\u7684\u5206\u5272\u65b9\u6848<br \/>\n    vector&lt;string&gt; path; \/\/ \u5b58\u50a8\u5f53\u524d\u5206\u5272\u65b9\u6848\u4e2d\u7684\u56de\u6587\u5b50\u4e32<br \/>\n    void backtracking (const string&amp; s, int startIndex) {<br \/>\n        \/\/ \u5982\u679c\u8d77\u59cb\u4f4d\u7f6e\u5df2\u7ecf\u5927\u4e8e\u7b49\u4e8es\u7684\u957f\u5ea6&#xff0c;\u8bf4\u660e\u5df2\u7ecf\u5206\u5272\u5230\u5b57\u7b26\u4e32\u672b\u5c3e<br \/>\n        if (startIndex &gt;&#061; s.size()) {<br \/>\n            result.push_back(path); \/\/ \u5c06\u5f53\u524d\u5206\u5272\u65b9\u6848\u52a0\u5165\u7ed3\u679c\u96c6<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/ \u4ecestartIndex\u5f00\u59cb\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u5206\u5272\u70b9<br \/>\n        for (int i &#061; startIndex; i &lt; s.size(); i&#043;&#043;) {<br \/>\n            \/\/ \u68c0\u67e5s[startIndex]\u5230s[i]\u7684\u5b50\u4e32\u662f\u5426\u662f\u56de\u6587<br \/>\n            if (isPalindrome(s, startIndex, i)) {<br \/>\n                \/\/ \u83b7\u53d6\u56de\u6587\u5b50\u4e32<br \/>\n                string str &#061; s.substr(startIndex, i &#8211; startIndex &#043; 1);<br \/>\n                path.push_back(str); \/\/ \u5c06\u56de\u6587\u5b50\u4e32\u52a0\u5165\u5f53\u524d\u8def\u5f84<br \/>\n            } else {                                \/\/ \u4e0d\u662f\u56de\u6587&#xff0c;\u8df3\u8fc7\u5f53\u524d\u5206\u5272\u70b9<br \/>\n                continue;<br \/>\n            }<br \/>\n            backtracking(s, i &#043; 1); \/\/ \u9012\u5f52\u5904\u7406\u5269\u4f59\u5b50\u4e32&#xff0c;\u4ecei&#043;1\u5f00\u59cb<br \/>\n            path.pop_back(); \/\/ \u56de\u6eaf&#xff1a;\u79fb\u9664\u6700\u540e\u6dfb\u52a0\u7684\u5b50\u4e32&#xff0c;\u5c1d\u8bd5\u5176\u4ed6\u5206\u5272\u53ef\u80fd<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5224\u65ad\u5b50\u4e32s[start&#8230;end]\u662f\u5426\u662f\u56de\u6587<br \/>\n    bool isPalindrome(const string&amp; s, int start, int end) {<br \/>\n        \/\/ \u53cc\u6307\u9488\u6cd5\u5224\u65ad\u56de\u6587<br \/>\n        for (int i &#061; start, j &#061; end; i &lt; j; i&#043;&#043;, j&#8211;) {<br \/>\n            if (s[i] !&#061; s[j]) {<br \/>\n                return false; \/\/ \u6709\u5b57\u7b26\u4e0d\u76f8\u7b49&#xff0c;\u4e0d\u662f\u56de\u6587<br \/>\n            }<br \/>\n        }<br \/>\n        return true; \/\/ \u6240\u6709\u5b57\u7b26\u90fd\u5bf9\u79f0&#xff0c;\u662f\u56de\u6587<br \/>\n    }<br \/>\npublic:<br \/>\n    vector&lt;vector&lt;string&gt;&gt; partition(string s) {<br \/>\n        \/\/ \u6e05\u7a7a\u7ed3\u679c\u96c6\u548c\u8def\u5f84&#xff0c;\u9632\u6b62\u591a\u6b21\u8c03\u7528\u65f6\u6570\u636e\u6b8b\u7559<br \/>\n        result.clear();<br \/>\n        path.clear();<br \/>\n        \/\/ \u4ece\u7d22\u5f150\u5f00\u59cb\u56de\u6eaf\u5206\u5272<br \/>\n        backtracking(s, 0);<br \/>\n        return result; \/\/ \u8fd4\u56de\u6240\u6709\u5206\u5272\u65b9\u6848<br \/>\n    }<br \/>\n};<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"706\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260123110422-697355b6518ae.png\" width=\"1221\" \/><\/p>\n<p>\u4e3e\u4e00\u4e2a\u5177\u4f53\u4f8b\u5b50&#xff0c;\u8f93\u5165&#xff1a;s &#061; &#034;aab&#034;&#xff0c;\u6765\u6a21\u62df\u4e0a\u8ff0\u4ee3\u7801\u6d41\u7a0b<\/p>\n<h3>\u521d\u59cb\u72b6\u6001<\/h3>\n<p> s &#061; &#034;aab&#034;<br \/>\n\u8c03\u7528 partition(&#034;aab&#034;) <\/p>\n<h3>\u7b2c1\u6b65&#xff1a;partition() \u51fd\u6570<\/h3>\n<p> result.clear() \u2192 result &#061; []<br \/>\npath.clear() \u2192 path &#061; []<br \/>\n\u8c03\u7528 backtracking(s, 0)<\/p>\n<h3>\u7b2c2\u6b65&#xff1a;backtracking(s, 0)<\/h3>\n<p> if (startIndex &gt;&#061; s.size()) {<br \/>\n     result.push_back(path); \/\/ \u5c06\u5f53\u524d\u5206\u5272\u65b9\u6848\u52a0\u5165\u7ed3\u679c\u96c6<br \/>\n     return;<br \/>\n}<br \/>\n startIndex &#061; 0 ,  s.size() &#061; 3<br \/>\nif (0 &gt;&#061; 3)\u4e0d\u6ee1\u8db3if\u8bed\u53e5<\/p>\n<p> for (int i &#061; startIndex; i &lt; s.size(); i&#043;&#043;) {<br \/>\n    \/\/ \u68c0\u67e5s[startIndex]\u5230s[i]\u7684\u5b50\u4e32\u662f\u5426\u662f\u56de\u6587<br \/>\n    if (isPalindrome(s, startIndex, i)) {<br \/>\n       \/\/ \u83b7\u53d6\u56de\u6587\u5b50\u4e32<br \/>\n       string str &#061; s.substr(startIndex, i &#8211; startIndex &#043; 1);<br \/>\n       path.push_back(str); \/\/ \u5c06\u56de\u6587\u5b50\u4e32\u52a0\u5165\u5f53\u524d\u8def\u5f84<br \/>\n    } else {  \/\/ \u4e0d\u662f\u56de\u6587&#xff0c;\u8df3\u8fc7\u5f53\u524d\u5206\u5272\u70b9<br \/>\n       continue;<br \/>\n    }<br \/>\n    backtracking(s, i &#043; 1); \/\/ \u9012\u5f52\u5904\u7406\u5269\u4f59\u5b50\u4e32&#xff0c;\u4ecei&#043;1\u5f00\u59cb<br \/>\n    path.pop_back(); \/\/ \u56de\u6eaf&#xff1a;\u79fb\u9664\u6700\u540e\u6dfb\u52a0\u7684\u5b50\u4e32&#xff0c;\u5c1d\u8bd5\u5176\u4ed6\u5206\u5272\u53ef\u80fd<br \/>\n}<br \/>\n for \u5faa\u73af\u5f00\u59cb&#xff1a;i&#061;0 \u5230 2<br \/>\n<span style=\"color:#fe2c24\">\u73b0\u5728i&#061;0<\/span><br \/>\n\u8c03\u7528 isPalindrome(s, 0, 0):<br \/>\n    \u6bd4\u8f83 s[0]&#061;&#039;a&#039; \u548c s[0]&#061;&#039;a&#039; \u2192 true<br \/>\nif (true) \u2192 \u8fdb\u5165if\u5757<br \/>\nstr &#061; s.substr(0, 1) &#061; &#034;a&#034;<br \/>\npath.push_back(&#034;a&#034;) \u2192 path &#061; [&#034;a&#034;]<br \/>\n\u8c03\u7528 backtracking(s, 1) <\/p>\n<\/p>\n<h3>\u7b2c3\u6b65&#xff1a;backtracking(s, 1)<\/h3>\n<p> startIndex &#061; 1<br \/>\nif (1 &gt;&#061; 3)\u4e0d\u6ee1\u8db3if\u6761\u4ef6<br \/>\n\u6267\u884cfor\u5faa\u73af<br \/>\nfor \u5faa\u73af\u5f00\u59cb&#xff1a;i &#061; 1 \u5230 2<br \/>\n<span style=\"color:#fe2c24\">\u73b0\u5728i&#061;1<br \/>\n<\/span><span style=\"color:#0d0016\">\u5224\u65ad<\/span>if (isPalindrome(s, startIndex, i))<br \/>\n\u51fd\u6570isPalindrome(s, 1, 1):<br \/>\n    \u6bd4\u8f83 s[1]&#061;&#039;a&#039; \u548c s[1]&#061;&#039;a&#039; \u2192 true<br \/>\nif (true) \u2192 \u6240\u4ee5\u8fdb\u5165if\u8bed\u53e5<\/p>\n<p> if (isPalindrome(s, startIndex, i)) {<br \/>\n    \/\/ \u83b7\u53d6\u56de\u6587\u5b50\u4e32<br \/>\n    string str &#061; s.substr(startIndex, i &#8211; startIndex &#043; 1);<br \/>\n    path.push_back(str); \/\/ \u5c06\u56de\u6587\u5b50\u4e32\u52a0\u5165\u5f53\u524d\u8def\u5f84<br \/>\n str &#061; s.substr(1, 1) &#061; &#034;a&#034;&#xff08;\u4eces[1]\u5f00\u59cb&#xff0c;\u53d6\u4e00\u4e2a\u5b57\u7b26&#xff09;<br \/>\npath.push_back(&#034;a&#034;) \u2192 path &#061; [&#034;a&#034;, &#034;a&#034;]<br \/>\n\u8c03\u7528 backtracking(s, 2) <\/p>\n<h3>\u7b2c4\u6b65&#xff1a;backtracking(s, 2)<\/h3>\n<p> startIndex &#061; 2<br \/>\nif (2 &gt;&#061; 3)\u4e0d\u6ee1\u8db3if\u6761\u4ef6<br \/>\n\u6240\u4ee5\u6267\u884cfor\u5faa\u73af<br \/>\nfor \u5faa\u73af\u5f00\u59cb&#xff1a;i &#061; 2 \u5230 2<br \/>\n<span style=\"color:#fe2c24\">\u73b0\u5728i&#061;2<\/span><br \/>\n\u8c03\u7528 isPalindrome(s, 2, 2):<br \/>\n    \u6bd4\u8f83 s[2]&#061;&#039;b&#039; \u548c s[2]&#061;&#039;b&#039; \u2192 true<br \/>\nif (true) \u2192 \u8fdb\u5165if\u8bed\u53e5<br \/>\nstr &#061; s.substr(2, 1) &#061; &#034;b&#034;&#xff08;\u4eces[2]\u53d61\u4e2a\u5b57\u7b26&#xff09;<br \/>\npath.push_back(&#034;b&#034;) \u2192 path &#061; [&#034;a&#034;, &#034;a&#034;, &#034;b&#034;]<br \/>\n\u8c03\u7528 backtracking(s, 3) <\/p>\n<h3>\u7b2c5\u6b65&#xff1a;backtracking(s, 3)<\/h3>\n<p> startIndex &#061; 3<br \/>\nif (3 &gt;&#061; 3)   \u6ee1\u8db3if\u8bed\u53e5true \u2192 \u8fdb\u5165if<\/p>\n<p> if (startIndex &gt;&#061; s.size()) {<br \/>\n     result.push_back(path); \/\/ \u5c06\u5f53\u524d\u5206\u5272\u65b9\u6848\u52a0\u5165\u7ed3\u679c\u96c6<br \/>\n     return;<br \/>\n}<br \/>\n result.push_back(path) \u2192 result &#061; [[&#034;a&#034;, &#034;a&#034;, &#034;b&#034;]]<br \/>\nreturn \u5230\u4e0a\u4e00\u5c42&#xff08;backtracking(s, 2)&#xff09; <\/p>\n<h3>\u7b2c6\u6b65&#xff1a;\u56de\u5230 backtracking(s, 2)<\/h3>\n<p>  backtracking(s, i &#043; 1); \/\/ \u9012\u5f52\u5904\u7406\u5269\u4f59\u5b50\u4e32&#xff0c;\u4ecei&#043;1\u5f00\u59cb<br \/>\n path.pop_back(); \/\/ \u56de\u6eaf&#xff1a;\u79fb\u9664\u6700\u540e\u6dfb\u52a0\u7684\u5b50\u4e32&#xff0c;\u5c1d\u8bd5\u5176\u4ed6\u5206\u5272\u53ef\u80fd<br \/>\n \u7ee7\u7eed\u6267\u884c backtracking(s, i&#043;1) \u540e\u9762\u7684\u4ee3\u7801&#xff1a;<br \/>\npath.pop_back() \u2192 \u4e4b\u540epath &#061; [&#034;a&#034;, &#034;a&#034;]<br \/>\ni&#043;&#043; \u2192 <span style=\"color:#fe2c24\">i&#061;3<\/span>&#xff0c;\u4e0d\u518d\u6ee1\u8db3for\u5faa\u73af\u6761\u4ef6&#xff0c;\u5faa\u73af\u7ed3\u675f<br \/>\nreturn \u5230\u4e0a\u4e00\u5c42&#xff08;backtracking(s, 1)&#xff09; <\/p>\n<h3>\u7b2c7\u6b65&#xff1a;\u56de\u5230 backtracking(s, 1)<\/h3>\n<p> <span style=\"color:#a5a5a5\">\/\/ \u7ee7\u7eed\u6267\u884c backtracking(s, i&#043;1) \u540e\u9762\u7684\u4ee3\u7801&#xff1a;<\/span><br \/>\n\/\/ \u8fd9\u91cc\u7684 i &#061; 1&#xff0c;\u56e0\u4e3a\u8fd9\u662f i &#061; 1 \u8fd9\u4e2a\u5faa\u73af\u5206\u652f<br \/>\npath.pop_back();<br \/>\n\u4e4b\u524d path &#061; [&#034;a&#034;, &#034;a&#034;]<br \/>\npop\u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20 \u2192 \u73b0\u5728path &#061; [&#034;a&#034;]<br \/>\n for \u5faa\u73af\u7ee7\u7eed  i&#043;&#043;;  <span style=\"color:#a5a5a5\">\/\/ i \u4ece 1 \u53d8\u6210 2<\/span><br \/>\n\u68c0\u67e5for\u5faa\u73af\u6761\u4ef6&#xff1a;i &lt; s.size() -&gt;  2 &lt; 3 \u6ee1\u8db3for\u5faa\u73af\u6761\u4ef6true<br \/>\n\u6240\u4ee5\u8fdb\u5165for\u5faa\u73af&#xff1a;<span style=\"color:#fe2c24\">\u73b0\u5728i &#061; 2 &#xff0c;<\/span>startIndex \u4ecd\u4e3a 1<br \/>\n for (int i &#061; startIndex; i &lt; s.size(); i&#043;&#043;) {<br \/>\n    \/\/ \u68c0\u67e5s[startIndex]\u5230s[i]\u7684\u5b50\u4e32\u662f\u5426\u662f\u56de\u6587<br \/>\n    if (isPalindrome(s, startIndex, i)) {<br \/>\n       \/\/ \u83b7\u53d6\u56de\u6587\u5b50\u4e32<br \/>\n       string str &#061; s.substr(startIndex, i &#8211; startIndex &#043; 1);<br \/>\n       path.push_back(str); \/\/ \u5c06\u56de\u6587\u5b50\u4e32\u52a0\u5165\u5f53\u524d\u8def\u5f84<br \/>\n    } else {  \/\/ \u4e0d\u662f\u56de\u6587&#xff0c;\u8df3\u8fc7\u5f53\u524d\u5206\u5272\u70b9<br \/>\n       continue;<br \/>\n    }<br \/>\n    backtracking(s, i &#043; 1); \/\/ \u9012\u5f52\u5904\u7406\u5269\u4f59\u5b50\u4e32&#xff0c;\u4ecei&#043;1\u5f00\u59cb<br \/>\n    path.pop_back(); \/\/ \u56de\u6eaf&#xff1a;\u79fb\u9664\u6700\u540e\u6dfb\u52a0\u7684\u5b50\u4e32&#xff0c;\u5c1d\u8bd5\u5176\u4ed6\u5206\u5272\u53ef\u80fd<br \/>\n}<br \/>\n \u8c03\u7528 isPalindrome(s, 1, 2):&#xff08;\u4eces[1]\u5f00\u59cb\u53d62\u4e2a\u5b57\u7b26&#xff09;&#034;ab&#034;<br \/>\n    \u6bd4\u8f83 s[1]&#061;&#039;a&#039; \u548c s[2]&#061;&#039;b&#039; \u2192 false &#xff08;&#034;ab&#034;\u4e0d\u662f\u56de\u6587&#xff0c;\u8fd4\u56de\u00a0false&#xff09;<br \/>\nif (false) \u2192 \u6267\u884ccontinue<br \/>\ni&#043;&#043; \u2192 i&#061;3&#xff0c;\u4e0d\u6ee1\u8db3for\u5faa\u73af\u6761\u4ef6&#xff0c;\u5faa\u73af\u7ed3\u675f\u3002for\u5faa\u73af\u7ed3\u675f\u540e&#xff0c;\u5373backtracking(s, 1)\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u3002\u6240\u4ee5\u8fd4\u56de\u5230\u8c03\u7528\u8005&#xff1a;backtracking(s, 0)<br \/>\n\u6267\u884c path.pop_back() \u2192 path &#061; [&#034;a&#034;]<br \/>\nreturn \u5230\u4e0a\u4e00\u5c42&#xff08;backtracking(s, 0)&#xff09;<br \/>\n \u79fb\u9664 &#034;a&#034;&#xff0c;path &#061; [] <\/p>\n<p>continue\u00a0\u8bed\u53e5\u7684\u4f5c\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u4e2d\u5269\u4f59\u7684\u4ee3\u7801<\/p>\n<\/li>\n<li>\n<p>\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u8fed\u4ee3<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u610f\u5473\u7740&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e0d\u4f1a\u6267\u884c\u00a0backtracking(s, i &#043; 1)&#xff08;\u5373\u00a0backtracking(s, 3)&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u4f1a\u6267\u884c\u8fd9\u4e2a\u5206\u652f\u7684\u00a0path.pop_back()<\/p>\n<\/li>\n<li>\n<p>\u76f4\u63a5\u8fdb\u5165\u00a0i&#043;&#043;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u7b2c8\u6b65&#xff1a;\u56de\u5230 backtracking(s, 0)<\/h3>\n<p> path.pop_back();  \u4e4b\u524d\u524d path &#061; [&#034;a&#034;]&#xff0c;\u73b0\u5728\u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20&#xff0c;\u73b0\u5728path &#061; []<br \/>\ni &#061; 0 \u7684\u5faa\u73af\u7ed3\u675f<br \/>\ni&#043;&#043; \u2192 <span style=\"color:#fe2c24\">\u73b0\u5728i &#061; 1<\/span><br \/>\n\u8c03\u7528 isPalindrome(s, 0, 1):<br \/>\n    \u6bd4\u8f83 s[0]&#061;&#039;a&#039; \u548c s[1]&#061;&#039;a&#039; \u2192 true<br \/>\nif (true) \u2192 \u8fdb\u5165if\u8bed\u53e5<br \/>\nstr &#061; s.substr(0, 2) &#061; &#034;aa&#034;<br \/>\npath.push_back(&#034;aa&#034;) \u2192 path &#061; [&#034;aa&#034;]<br \/>\n\u8c03\u7528 backtracking(s, 2) <\/p>\n<h3>\u7b2c9\u6b65&#xff1a;backtracking(s, 2)<\/h3>\n<p> startIndex &#061; 2<br \/>\nif (2 &gt;&#061; 3) \u4e0d\u6ee1\u8db3if\u6761\u4ef6\u5224\u65ad<\/p>\n<p>for \u5faa\u73af\u5f00\u59cb&#xff1a;i &#061; 2 \u5230 2<br \/>\n\u8c03\u7528 isPalindrome(s, 2, 2):<br \/>\n    \u6bd4\u8f83 s[2]&#061;&#039;b&#039; \u548c s[2]&#061;&#039;b&#039; \u2192 true<br \/>\nif (true) \u2192 \u8fdb\u5165if\u5757<br \/>\nstr &#061; s.substr(2, 1) &#061; &#034;b&#034;<br \/>\npath.push_back(&#034;b&#034;) \u2192 path &#061; [&#034;aa&#034;, &#034;b&#034;]<br \/>\n\u8c03\u7528 backtracking(s, 3) <\/p>\n<p>&#8230;&#8230;&#8230;<\/p>\n<p>&#8230;&#8230;&#8230;<\/p>\n<p>&#8230;&#8230;&#8230;<\/p>\n<p>&#8230;&#8230;&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>bool isPalindrome(string &amp;s, int start, int end)  \/\/ \u2705 \u5f15\u7528\u4f20\u9012<br \/>\n\u56e0\u4e3a&#xff1a; isPalindrome\u00a0\u53ea\u662f\u8bfb\u53d6\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u4fee\u6539\u5b83  \u4f46\u6700\u597d\u52a0\u4e0a\u00a0const&#xff1a;bool isPalindrome(const string &amp;s, &#8230;)<br \/>\n\/\/ \u4f7f\u7528 const &amp;&#xff0c;\u56e0\u4e3a\u4e0d\u4fee\u6539\u5b57\u7b26\u4e32&#xff0c;\u53ea\u8bfb\u53d6<br \/>\nvoid backtracking(const string &amp;<\/p>\n","protected":false},"author":2,"featured_media":64557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6831,2692,1813,427],"topic":[],"class_list":["post-64559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-6831","tag-leetcode","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \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\/64559.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"bool isPalindrome(string &amp;s, int start, int end) \/\/ \u2705 \u5f15\u7528\u4f20\u9012 \u56e0\u4e3a&#xff1a; isPalindrome\u00a0\u53ea\u662f\u8bfb\u53d6\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u4fee\u6539\u5b83 \u4f46\u6700\u597d\u52a0\u4e0a\u00a0const&#xff1a;bool isPalindrome(const string &amp;s, ...)  \/\/ \u4f7f\u7528 const &amp;&#xff0c;\u56e0\u4e3a\u4e0d\u4fee\u6539\u5b57\u7b26\u4e32&#xff0c;\u53ea\u8bfb\u53d6 void backtracking(const string &amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/64559.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-23T11:04:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260123110422-697355b621019.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64559.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/64559.html\",\"name\":\"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-23T11:04:23+00:00\",\"dateModified\":\"2026-01-23T11:04:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/64559.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/64559.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64559.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09\"}]},{\"@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\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \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\/64559.html","og_locale":"zh_CN","og_type":"article","og_title":"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"bool isPalindrome(string &amp;s, int start, int end) \/\/ \u2705 \u5f15\u7528\u4f20\u9012 \u56e0\u4e3a&#xff1a; isPalindrome\u00a0\u53ea\u662f\u8bfb\u53d6\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u4fee\u6539\u5b83 \u4f46\u6700\u597d\u52a0\u4e0a\u00a0const&#xff1a;bool isPalindrome(const string &amp;s, ...)  \/\/ \u4f7f\u7528 const &amp;&#xff0c;\u56e0\u4e3a\u4e0d\u4fee\u6539\u5b57\u7b26\u4e32&#xff0c;\u53ea\u8bfb\u53d6 void backtracking(const string &amp;","og_url":"https:\/\/www.wsisp.com\/helps\/64559.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-23T11:04:23+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260123110422-697355b621019.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/64559.html","url":"https:\/\/www.wsisp.com\/helps\/64559.html","name":"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-23T11:04:23+00:00","dateModified":"2026-01-23T11:04:23+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/64559.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/64559.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/64559.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"leetcode\u56de\u6eaf\u7b97\u6cd5\uff08131.\u5206\u5272\u56de\u6587\u4e32\uff09"}]},{"@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\/64559","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=64559"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/64559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/64557"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=64559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=64559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=64559"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=64559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}