{"id":573,"date":"2020-09-26T10:59:07","date_gmt":"2020-09-26T02:59:07","guid":{"rendered":"http:\/\/39.96.58.60\/?p=573"},"modified":"2022-10-18T16:38:45","modified_gmt":"2022-10-18T08:38:45","slug":"leetcode-647-%e5%9b%9e%e6%96%87%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=573","title":{"rendered":"Leetcode 647. \u56de\u6587\u5b50\u4e32"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f60\u7684\u4efb\u52a1\u662f\u8ba1\u7b97\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6709\u591a\u5c11\u4e2a\u56de\u6587\u5b50\u4e32\u3002<\/p>\n<p>\u5177\u6709\u4e0d\u540c\u5f00\u59cb\u4f4d\u7f6e\u6216\u7ed3\u675f\u4f4d\u7f6e\u7684\u5b50\u4e32\uff0c\u5373\u4f7f\u662f\u7531\u76f8\u540c\u7684\u5b57\u7b26\u7ec4\u6210\uff0c\u4e5f\u4f1a\u88ab\u89c6\u4f5c\u4e0d\u540c\u7684\u5b50\u4e32\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"abc\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e09\u4e2a\u56de\u6587\u5b50\u4e32: \"a\", \"b\", \"c\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"aaa\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>6\n\n<strong>\u89e3\u91ca\uff1a<\/strong>6\u4e2a\u56de\u6587\u5b50\u4e32: \"a\", \"a\", \"a\", \"aa\", \"aa\", \"aaa\"<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 1000 \u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001 \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a648 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e869.86% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8679.30% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u66b4\u529b\u6cd5, \u53cc\u6307\u9488\u66b4\u529b\u62c6\u89e3\n\"\"\"\nclass Solution:\n    def countSubstrings(self, s: str) -&gt; int:\n        n = len(s)\n        num = 0\n        for i in range(n):\n            for j in range(i, n):\n                string = s[i:j+1]\n                if string == string[::-1]:\n                    num += 1\n        return num\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a164 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8665.42% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8662.96% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u4e2d\u5fc3\u6269\u5c55, \u9700\u5206\u6790\u4e24\u79cd\u60c5\u51b5    xax, xaax \u4ee5\u5355\u4e2a\u5b57\u7b26\u4e3a\u4e2d\u5fc3\uff0c\u4ee5\u4e24\u4e2a\u5b57\u7b26\u4e3a\u4e2d\u5fc3\n    \u8be6\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def countSubstrings(self, s: str) -&gt; int:\n        n = len(s)\n        num = 0\n        i = 0\n        while i &lt; n:        # i\u6307\u9488\uff0c\u4f9d\u6b21\u904d\u5386\u5b57\u7b26\u4e32\n            num += 1        # \u6bcf\u4e2a\u5355\u72ec\u7684\u5b57\u7b26\u90fd\u56de\u6587\n            l, r = i, i     # \u4ee5\u5f53\u524d\u5b57\u7b26\u4e3a\u4e2d\u5fc3\uff0c\u5411\u4e24\u4fa7\u6269\u5c55\n            while l-1 &gt;= 0 and r+1 &lt; n:\n                if s[l-1] == s[r+1]:    # \u5224\u65ad\u5b57\u7b26\u4e24\u4fa7\u662f\u5426\u76f8\u540c\uff0c\u82e5\u76f8\u540c\uff0c\u5219\u8be5\u5b57\u7b26\u4e32\u56de\u6587\n                    num += 1\n                    l = l-1             # \u76f8\u540c\u7ee7\u7eed\u6269\u5c55\n                    r = r+1\n                else:\n                    break               # \u4e0d\u76f8\u540c\uff0c\u5219\u8df3\u51fa\u5faa\u73af\uff0c\u904d\u5386\u4e0b\u4e00\u4e2a\u5b57\u7b26\n\n            if i+1<n and=\"\" s[i+1]=\"=\" s[i]:=\"\" #=\"\" \u5224\u65ad\u5f53\u524d\u70b9\u4e0e\u4e0b\u4e00\u4e2a\u70b9\u662f\u5426\u76f8\u540c\uff0c\u82e5\u76f8\u540c\uff0c\u5219\u76f8\u90bb\u4e24\u5b57\u7b26\u56de\u6587=\"\" num=\"\" +=\"1\" \u82e5\u76f8\u540c\uff0c\u5219\u76f8\u90bb\u4e24\u5b57\u7b26\u56de\u6587=\"\" l,=\"\" r=\"i,\" i+1=\"\" \u4ee5\u4e24\u5b57\u7b26\u5f00\u59cb\u5411\u5916\u6269\u5c55=\"\" while=\"\" l=\"\" -=\"\" 1=\"\">= 0 and r + 1 &lt; n:\n                    if s[l - 1] == s[r + 1]:    # \u4e24\u4fa7\u5b57\u7b26\u76f8\u540c\uff0c\u5219\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u56de\u6587\n                        num += 1\n                        l = l - 1               # \u7ee7\u7eed\u6269\u5c55\n                        r = r + 1\n                    else:\n                        break\n            i += 1\n        return num\n\n<\/n><\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f60\u7684\u4efb\u52a1\u662f\u8ba1\u7b97\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6709\u591a\u5c11\u4e2a\u56de\u6587\u5b50\u4e32\u3002 \u5177\u6709\u4e0d\u540c\u5f00\u59cb\u4f4d\u7f6e\u6216\u7ed3\u675f\u4f4d\u7f6e\u7684\u5b50\u4e32\uff0c\u5373\u4f7f\u662f\u7531\u76f8\u540c\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/573"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=573"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":574,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions\/574"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}