{"id":49256,"date":"2024-12-02T14:59:43","date_gmt":"2024-12-02T06:59:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49256\/"},"modified":"2024-12-02T14:59:43","modified_gmt":"2024-12-02T06:59:43","slug":"%e6%89%be%e5%88%b0%e6%9c%80%e8%bf%91%e7%9a%84%e5%9b%9e%e6%96%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49256\/","title":{"rendered":"\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173225462967401ba53f6fd.jpg\" class=\"aligncenter\" title=\"\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587\u63d2\u56fe\" alt=\"\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587\u63d2\u56fe\" \/><\/p>\n<p>564\u3002\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587<\/p>\n<p><strong>\u96be\u5ea6\uff1a<\/strong> \u96be<\/p>\n<p><strong>\u4e3b\u9898\uff1a<\/strong>\u6570\u5b66\u3001\u5b57\u7b26\u4e32<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u8868\u793a\u6574\u6570\u7684\u5b57\u7b26\u4e32 n\uff0c\u8fd4\u56de_\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff08\u4e0d\u5305\u62ec\u5176\u81ea\u8eab\uff09\uff0c\u8fd9\u662f\u4e00\u4e2a\u56de\u6587-\u3002\u5982\u679c\u6709\u5e73\u5c40\uff0c\u5219\u8fd4\u56de<strong>\u8f83\u5c0f\u7684<\/strong>\u3002<\/p>\n<p>\u6700\u63a5\u8fd1\u7684\u5b9a\u4e49\u4e3a\u4e24\u4e2a\u6574\u6570\u4e4b\u95f4\u7684\u7edd\u5bf9\u5dee\u6700\u5c0f\u5316\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> n = &#8220;123&#8221;<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong>\u201c121\u201d<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> n = &#8220;1&#8221;<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong>\u201c0\u201d<\/li>\n<li> <strong>\u89e3\u91ca\uff1a<\/strong> 0 \u548c 2 \u662f\u6700\u63a5\u8fd1\u7684\u56de\u6587\uff0c\u4f46\u6211\u4eec\u8fd4\u56de\u6700\u5c0f\u7684 0\u3002<\/li>\n<\/ul>\n<p><strong>\u9650\u5236\uff1a<\/strong><\/p>\n<ul>\n<li>1 &lt;= n.length &lt;= 18<\/li>\n<li> n \u4ec5\u7531\u6570\u5b57\u7ec4\u6210\u3002<\/li>\n<li> n \u6ca1\u6709\u524d\u5bfc\u96f6\u3002<\/li>\n<li> n \u8868\u793a [1, 10<sup>18<\/sup> &#8211; 1] \u8303\u56f4\u5185\u7684\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li>\u66b4\u529b\u7834\u89e3\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f\u60f3\u70b9\u522b\u7684\u5427\u3002<\/li>\n<li>\u4e3e\u4e00\u4e9b\u4f8b\u5b50\uff0c\u6bd4\u5982 1234\u3001999\u30011000 \u7b49\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u6700\u63a5\u8fd1\u7684\u56de\u6587\u3002\u53ef\u80fd\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u60c5\u51b5\uff1f<\/li>\n<li>\u6211\u4eec\u662f\u5426\u5fc5\u987b\u53ea\u8003\u8651\u5b57\u7b26\u4e32\u7684\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\uff0c\u8fd8\u662f\u4e24\u8005\u90fd\u8003\u8651\uff1f<\/li>\n<li>\u5c1d\u8bd5\u627e\u5230\u8fd9\u4e9b\u6570\u5b57\u4e2d\u6700\u63a5\u8fd1\u7684\u56de\u6587\u6570 &#8211; 12932\u300199800\u300112120\u3002\u4f60\u89c2\u5bdf\u5230\u4ec0\u4e48\u4e86\u5417\uff1f<\/li>\n<\/ol>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u6f5c\u5728\u7684\u56de\u6587\u5019\u9009\uff0c\u7136\u540e\u9009\u62e9\u6700\u63a5\u8fd1\u8f93\u5165\u6570\u5b57\u7684\u4e00\u4e2a\u3002<\/p>\n<h3> \u89e3\u51b3\u529e\u6cd5\uff1a <\/h3>\n<ol>\n<li>\n<p><strong>\u8bc6\u522b\u56de\u6587\u5019\u9009\u8005<\/strong>:<\/p>\n<ul>\n<li>\u955c\u50cf\u6570\u5b57\u7684\u524d\u534a\u90e8\u5206\u5f62\u6210\u56de\u6587\u3002<\/li>\n<li>\u8003\u8651\u8fb9\u7f18\u60c5\u51b5\uff0c\u4f8b\u5982\u6240\u6709\u6570\u5b57\u5747\u4e3a 9\u3001100&#8230;001 \u6216 99&#8230;99\u3002<\/li>\n<li>\u901a\u8fc7\u5c06\u6570\u5b57\u4e2d\u95f4\u5411\u4e0a\u6216\u5411\u4e0b\u4fee\u6539 1 \u6765\u751f\u6210\u56de\u6587\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u6700\u63a5\u8fd1\u7684\u56de\u6587<\/strong>:<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u56de\u6587\u5019\u9009\uff0c\u8ba1\u7b97\u4e0e\u539f\u59cb\u6570\u5b57\u7684\u7edd\u5bf9\u5dee\u3002<\/li>\n<li>\u8fd4\u56de\u5dee\u5f02\u6700\u5c0f\u7684\u56de\u6587\u3002\u5982\u679c\u5b58\u5728\u5e73\u5c40\uff0c\u5219\u8fd4\u56de\u8f83\u5c0f\u7684\u56de\u6587\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>564\u3002\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587<\/strong><\/p>\n<pre>&lt;?php\n\/**\n* @param string $n\n* @return string\n*\/\nfunction nearestpalindromic($n) {\n    ...\n    ...\n    ...\n    \/**\n     * go to https:\/\/github.com\/mah-shamim\/leet-code-in-php\/tree\/main\/algorithms\/000564-find-the-closest-palindrome\/solution.php\n     *\/\n}\n\nfunction generatepalindrome($firsthalf, $isoddlength) {\n    ...\n    ...\n    ...\n}\n\n\/\/ example usage\necho nearestpalindromic(\"123\"); \/\/ output: \"121\"\necho nearestpalindromic(\"1\");   \/\/ output: \"0\"\n?&gt;\n<\/pre>\n<h3> \u89e3\u91ca\uff1a <\/h3>\n<ul>\n<li> <strong>\u751f\u6210\u56de\u6587\uff08$firsthalf\uff0c$isoddlength\uff09<\/strong>\uff1a\n<ul>\n<li>\u6b64\u8f85\u52a9\u51fd\u6570\u901a\u8fc7\u955c\u50cf\u6570\u5b57\u7684\u524d\u534a\u90e8\u5206\u6765\u521b\u5efa\u56de\u6587\u3002 <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>&lt;?php\n\/**\n* @param $firstHalf\n* @param $isOddLength\n* @return string\n*\/\nfunction generatePalindrome($firstHalf, $isOddLength) {\n    $secondHalf = strrev(substr($firstHalf, 0, $isOddLength ? -1 : $firstHalf));\n    return $firstHalf . $secondHalf;\n}\n?&gt;\n<\/pre>\n<ul>\n<li>\n<p><strong>\u8fb9\u7f18\u6848\u4f8b<\/strong>:<\/p>\n<ul>\n<li>\u901a\u8fc7\u663e\u5f0f\u68c0\u67e5\u8fd9\u4e9b\u60c5\u51b5\u6765\u5904\u7406\u4ece 100&#8230;001 \u6216 99&#8230;99 \u7b49\u6570\u5b57\u751f\u6210\u7684\u56de\u6587\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4e3b\u8981\u903b\u8f91<\/strong>:<\/p>\n<ul>\n<li>\u6211\u4eec\u8ba1\u7b97\u53ef\u80fd\u7684\u56de\u6587\uff0c\u7136\u540e\u901a\u8fc7\u6bd4\u8f83\u7edd\u5bf9\u5dee\u6765\u627e\u5230\u6700\u63a5\u8fd1\u7684\u56de\u6587\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8be5\u89e3\u51b3\u65b9\u6848\u6709\u6548\u5730\u7f29\u5c0f\u4e86\u53ef\u80fd\u7684\u56de\u6587\u5019\u9009\u8303\u56f4\uff0c\u5e76\u901a\u8fc7\u4ec5\u8003\u8651\u51e0\u4e2a\u9009\u9879\u6765\u9009\u62e9\u6700\u63a5\u8fd1\u7684\u4e00\u4e2a\uff0c\u4f7f\u5176\u6bd4\u66b4\u529b\u65b9\u6cd5\u5feb\u5f97\u591a\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\u9897\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>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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>\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587 \u6536\u85cf \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 564\u3002\u627e\u5230\u6700\u8fd1\u7684\u56de\u6587 \u96be\u5ea6\uff1a \u96be \u4e3b\u9898\uff1a\u6570\u5b66\u3001\u5b57\u7b26\u4e32 \u7ed9\u5b9a\u4e00\u4e2a\u8868\u793a\u6574\u6570\u7684\u5b57\u7b26\u4e32 n\uff0c\u8fd4\u56de_\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff08\u4e0d\u5305\u62ec\u5176\u81ea\u8eab\uff09\uff0c\u8fd9\u662f\u4e00\u4e2a\u56de\u6587-\u3002\u5982\u679c\u6709\u5e73\u5c40\uff0c\u5219\u8fd4\u56de\u8f83\u5c0f\u7684\u3002 \u6700\u63a5\u8fd1\u7684\u5b9a\u4e49\u4e3a\u4e24\u4e2a\u6574\u6570\u4e4b\u95f4\u7684\u7edd\u5bf9\u5dee\u6700\u5c0f\u5316\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1a n = &#8220;123&#8221; \u8f93\u51fa\uff1a\u201c121\u201d \u793a\u4f8b2\uff1a \u8f93\u5165\uff1a n = &#8220;1&#8221; \u8f93\u51fa\uff1a\u201c0\u201d \u89e3\u91ca\uff1a 0 \u548c 2 \u662f\u6700\u63a5\u8fd1\u7684\u56de\u6587\uff0c\u4f46\u6211\u4eec\u8fd4\u56de\u6700\u5c0f\u7684 0\u3002 \u9650\u5236\uff1a 1 &lt;= n.length &lt;= 18 n \u4ec5\u7531\u6570\u5b57\u7ec4\u6210\u3002 n \u6ca1\u6709\u524d\u5bfc\u96f6\u3002 n \u8868\u793a [1, 1018 &#8211; 1] \u8303\u56f4\u5185\u7684\u6574\u6570\u3002 \u63d0\u793a\uff1a \u66b4\u529b\u7834\u89e3\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f\u60f3\u70b9\u522b\u7684\u5427\u3002 \u4e3e\u4e00\u4e9b\u4f8b\u5b50\uff0c\u6bd4\u5982 1234\u3001999\u30011000 \u7b49\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u6700\u63a5\u8fd1\u7684\u56de\u6587\u3002\u53ef\u80fd\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u60c5\u51b5\uff1f \u6211\u4eec\u662f\u5426\u5fc5\u987b\u53ea\u8003\u8651\u5b57\u7b26\u4e32\u7684\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\uff0c\u8fd8\u662f\u4e24\u8005\u90fd\u8003\u8651\uff1f \u5c1d\u8bd5\u627e\u5230\u8fd9\u4e9b\u6570\u5b57\u4e2d\u6700\u63a5\u8fd1\u7684\u56de\u6587\u6570 &#8211; 12932\u300199800\u300112120\u3002\u4f60\u89c2\u5bdf\u5230\u4ec0\u4e48\u4e86\u5417\uff1f \u89e3\u51b3\u65b9\u6848\uff1a [&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-49256","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49256","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=49256"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49256\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}