{"id":38343,"date":"2024-11-26T15:18:00","date_gmt":"2024-11-26T07:18:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38343\/"},"modified":"2024-11-26T15:18:00","modified_gmt":"2024-11-26T07:18:00","slug":"leetcode-%e9%a1%b6%e7%ba%a7%e8%ae%bf%e8%b0%88-%e6%9c%89%e6%95%88%e5%9b%9e%e6%96%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38343\/","title":{"rendered":"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u6709\u6548\u56de\u6587"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173156193824554.jpg\" class=\"aligncenter\" title=\"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u6709\u6548\u56de\u6587\u63d2\u56fe\" alt=\"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u6709\u6548\u56de\u6587\u63d2\u56fe\" \/><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\uff0c\u63cf\u8ff0\u4e3a\uff1a<\/p>\n<blockquote><p>\n  \u5982\u679c\u4e00\u4e2a\u77ed\u8bed\u5728\u5c06\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u5e76\u5220\u9664\u6240\u6709\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u540e\uff0c\u5411\u524d\u548c\u5411\u540e\u8bfb\u53d6\u76f8\u540c\u7684\u5185\u5bb9\uff0c\u5219\u8be5\u77ed\u8bed\u662f\u56de\u6587\u3002\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u5305\u62ec\u5b57\u6bcd\u548c\u6570\u5b57\u3002 \u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u5982\u679c\u5b83\u662f\u56de\u6587\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1as =\u201c\u4e00\u4e2a\u4eba\uff0c\u4e00\u4e2a\u8ba1\u5212\uff0c\u4e00\u6761\u8fd0\u6cb3\uff1a\u5df4\u62ff\u9a6c\u201d \u8f93\u51fa\uff1atrue \u8bf4\u660e\uff1a\u201camanaplanacanalpanama\u201d\u662f\u4e00\u4e2a\u56de\u6587\u3002 \u793a\u4f8b2\uff1a \u8f93\u5165\uff1as =\u201c\u8d5b\u8f66\u201d \u8f93\u51fa\uff1a\u5047 \u8bf4\u660e\uff1a\u201craceacar\u201d\u4e0d\u662f\u56de\u6587\u3002 \u793a\u4f8b3\uff1a \u8f93\u5165\uff1as = &#8221; &#8221; \u8f93\u51fa\uff1atrue \u89e3\u91ca\uff1as \u662f\u5220\u9664\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u540e\u7684\u7a7a\u5b57\u7b26\u4e32 &#8220;&#8221;\u3002 \u7531\u4e8e\u7a7a\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u53d6\u76f8\u540c\uff0c\u56e0\u6b64\u5b83\u662f\u56de\u6587\u3002 \u9650\u5236\uff1a 1\n<\/p><\/blockquote>\n<p>\u5728\u56de\u6587\u4e4b\u540e\uff0c\u60a8\u57fa\u672c\u4e0a\u5220\u9664\u6240\u6709\u975e alpha\uff0c\u7136\u540e\u8c03\u7528\u964d\u4f4e\u5927\u5c0f\u5199\u65b9\u6cd5\u5e76\u8fdb\u884c\u6062\u590d\u4ee5\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u548c\u4e00\u4e9b reg exp \u4f60\u53ef\u4ee5\u7528 3 \u884c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre>class Solution {\n    public boolean isPalindrome(String s) {\n        final String reduced = s.replaceAll(\"[^A-Za-z0-9]\", \"\").toLowerCase();\n        final String inverted = new StringBuilder(reduced).reverse().toString();\n        return reduced.equals(inverted);\n    }\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<blockquote>\n<p>\u8fd0\u884c\u65f6\u95f4\uff1a15 \u6beb\u79d2\uff0c\u6bd4 valid palindrome \u7684 java \u5728\u7ebf\u63d0\u4ea4\u7684 22.51% \u5feb\u3002<\/p>\n<p>\u5185\u5b58\u4f7f\u7528\uff1a44.8 mb\uff0c\u4f4e\u4e8e\u6709\u6548\u56de\u6587 java \u5728\u7ebf\u63d0\u4ea4\u7684 34.15%\u3002<\/p>\n<\/blockquote>\n<p>\u4f5c\u4e3a\u7b80\u5355\u800c\u7b80\u5355\u7684\u95ee\u9898\uff0c\u8fd9\u5e94\u8be5\u8db3\u591f\u4e86\uff0c\u4f46\u662f\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u60a8\u53ef\u4ee5\u8fed\u4ee3\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u5e76\u6839\u636e\u9700\u8981\u5220\u9664\u548c\u8c03\u6574\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u8bf7\u67e5\u770b leetcode \u4e0a\u7684 saksham \u7b54\u6848\uff08\u5982\u679c\u60a8\u5e0c\u671b\u6211\u8be6\u7ec6\u89e3\u91ca\uff09 \uff0c\u8bf7\u5728\u4e0b\u9762\u8bc4\u8bba\uff09\u3002<\/p>\n<p>\u2014<\/p>\n<p>\u5c31\u662f\u8fd9\u6837\uff01\u5982\u679c\u8fd8\u6709\u4ec0\u4e48\u8981\u8ba8\u8bba\u7684\uff0c\u8bf7\u968f\u65f6\u53d1\u8868\u8bc4\u8bba\uff0c\u5982\u679c\u6211\u9519\u8fc7\u4e86\u4efb\u4f55\u5185\u5bb9\uff0c\u8bf7\u544a\u8bc9\u6211\uff0c\u4ee5\u4fbf\u6211\u8fdb\u884c\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n<p>\u76f4\u5230\u4e0b\u4e00\u7bc7\u6587\u7ae0\uff01 \ud83d\ude42<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLeetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u6709\u6548\u56de\u6587\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\uff0c\u63cf\u8ff0\u4e3a\uff1a \u5982\u679c\u4e00\u4e2a\u77ed\u8bed\u5728\u5c06\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u5e76\u5220\u9664\u6240\u6709\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u540e\uff0c\u5411\u524d\u548c\u5411\u540e\u8bfb\u53d6\u76f8\u540c\u7684\u5185\u5bb9\uff0c\u5219\u8be5\u77ed\u8bed\u662f\u56de\u6587\u3002\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u5305\u62ec\u5b57\u6bcd\u548c\u6570\u5b57\u3002 \u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u5982\u679c\u5b83\u662f\u56de\u6587\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1as =\u201c\u4e00\u4e2a\u4eba\uff0c\u4e00\u4e2a\u8ba1\u5212\uff0c\u4e00\u6761\u8fd0\u6cb3\uff1a\u5df4\u62ff\u9a6c\u201d \u8f93\u51fa\uff1atrue \u8bf4\u660e\uff1a\u201camanaplanacanalpanama\u201d\u662f\u4e00\u4e2a\u56de\u6587\u3002 \u793a\u4f8b2\uff1a \u8f93\u5165\uff1as =\u201c\u8d5b\u8f66\u201d \u8f93\u51fa\uff1a\u5047 \u8bf4\u660e\uff1a\u201craceacar\u201d\u4e0d\u662f\u56de\u6587\u3002 \u793a\u4f8b3\uff1a \u8f93\u5165\uff1as = &#8221; &#8221; \u8f93\u51fa\uff1atrue \u89e3\u91ca\uff1as \u662f\u5220\u9664\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u540e\u7684\u7a7a\u5b57\u7b26\u4e32 &#8220;&#8221;\u3002 \u7531\u4e8e\u7a7a\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u53d6\u76f8\u540c\uff0c\u56e0\u6b64\u5b83\u662f\u56de\u6587\u3002 \u9650\u5236\uff1a 1 \u5728\u56de\u6587\u4e4b\u540e\uff0c\u60a8\u57fa\u672c\u4e0a\u5220\u9664\u6240\u6709\u975e alpha\uff0c\u7136\u540e\u8c03\u7528\u964d\u4f4e\u5927\u5c0f\u5199\u65b9\u6cd5\u5e76\u8fdb\u884c\u6062\u590d\u4ee5\u8fdb\u884c\u6bd4\u8f83\u3002 \u4f7f\u7528\u65b9\u6cd5\u548c\u4e00\u4e9b reg exp \u4f60\u53ef\u4ee5\u7528 3 \u884c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a class Solution { public boolean isPalindrome(String s) { final String reduced = s.replaceAll(&#8220;[^A-Za-z0-9]&#8221;, &#8220;&#8221;).toLowerCase(); final String inverted = new [&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-38343","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38343","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=38343"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38343\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}