{"id":1740,"date":"2022-01-11T10:59:48","date_gmt":"2022-01-11T02:59:48","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1740"},"modified":"2022-01-11T10:59:49","modified_gmt":"2022-01-11T02:59:49","slug":"leetcode10","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/11\/leetcode10\/","title":{"rendered":"leetcode10 &#8211;\u6b63\u5219\u5339\u914d"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\u548c\u4e00\u4e2a\u5b57\u7b26\u89c4\u5f8b&nbsp;<code>p<\/code>\uff0c\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301&nbsp;<code>'.'<\/code>&nbsp;\u548c&nbsp;<code>'*'<\/code>&nbsp;\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002<\/p>\n\n\n\n<ul><li><code>'.'<\/code>&nbsp;\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/li><li><code>'*'<\/code>&nbsp;\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u524d\u9762\u7684\u90a3\u4e00\u4e2a\u5143\u7d20<\/li><\/ul>\n\n\n\n<p>\u6240\u8c13\u5339\u914d\uff0c\u662f\u8981\u6db5\u76d6&nbsp;<strong>\u6574\u4e2a&nbsp;<\/strong>\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>\u7684\uff0c\u800c\u4e0d\u662f\u90e8\u5206\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"aa\" p = \"a\"\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n<strong>\u89e3\u91ca\uff1a<\/strong>\"a\" \u65e0\u6cd5\u5339\u914d \"aa\" \u6574\u4e2a\u5b57\u7b26\u4e32\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"aa\" p = \"a*\"\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n<strong>\u89e3\u91ca\uff1a<\/strong>\u56e0\u4e3a '*' \u4ee3\u8868\u53ef\u4ee5\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u524d\u9762\u7684\u90a3\u4e00\u4e2a\u5143\u7d20, \u5728\u8fd9\u91cc\u524d\u9762\u7684\u5143\u7d20\u5c31\u662f 'a'\u3002\u56e0\u6b64\uff0c\u5b57\u7b26\u4e32 \"aa\" \u53ef\u88ab\u89c6\u4e3a 'a' \u91cd\u590d\u4e86\u4e00\u6b21\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>s = \"ab\" p = \".*\"\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n<strong>\u89e3\u91ca\uff1a<\/strong>\".*\" \u8868\u793a\u53ef\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\uff08'*'\uff09\u4efb\u610f\u5b57\u7b26\uff08'.'\uff09\u3002\u3001\n\n\n\u793a\u4f8b 4\uff1a\n\n\u8f93\u5165\uff1as = \"aab\" p = \"c*a*b\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u56e0\u4e3a '*' \u8868\u793a\u96f6\u4e2a\u6216\u591a\u4e2a\uff0c\u8fd9\u91cc 'c' \u4e3a 0 \u4e2a, 'a' \u88ab\u91cd\u590d\u4e00\u6b21\u3002\u56e0\u6b64\u53ef\u4ee5\u5339\u914d\u5b57\u7b26\u4e32 \"aab\"\u3002\n\u793a\u4f8b 5\uff1a\n\n\u8f93\u5165\uff1as = \"mississippi\" p = \"mis*is*p*.\"\n\u8f93\u51fa\uff1afalse\n\n<\/code><\/pre>\n\n\n\n<p>\u601d\u8def\uff1a\u52a8\u6001\u89c4\u5212\u6c42\u89e3<\/p>\n\n\n\n<p>\u4e00\u5f00\u59cb\u6ca1\u6709\u601d\u8def\uff0c\u56e0\u6b64\uff0c\u770b\u4e86\u5b98\u65b9\u6559\u7a0b\uff0c\u7136\u540e\u81ea\u5df1\u5f00\u59cb\u52a8\u624b\u5199\u3002<\/p>\n\n\n\n<p>\u601d\u8def\u4e0e\u7b97\u6cd5<\/p>\n\n\n\n<p>\u9898\u76ee\u4e2d\u7684\u5339\u914d\u662f\u4e00\u4e2a\u300c\u9010\u6b65\u5339\u914d\u300d\u7684\u8fc7\u7a0b\uff1a\u6211\u4eec\u6bcf\u6b21\u4ece\u5b57\u7b26\u4e32 p \u4e2d\u53d6\u51fa\u4e00\u4e2a\u5b57\u7b26\u6216\u8005\u300c\u5b57\u7b26 + \u661f\u53f7\u300d\u7684\u7ec4\u5408\uff0c\u5e76\u5728 s \u4e2d\u8fdb\u884c\u5339\u914d\u3002\u5bf9\u4e8e p \u4e2d\u4e00\u4e2a\u5b57\u7b26\u800c\u8a00\uff0c\u5b83\u53ea\u80fd\u5728 s \u4e2d\u5339\u914d\u4e00\u4e2a\u5b57\u7b26\uff0c\u5339\u914d\u7684\u65b9\u6cd5\u5177\u6709\u552f\u4e00\u6027\uff1b\u800c\u5bf9\u4e8e p \u4e2d\u5b57\u7b26 + \u661f\u53f7\u7684\u7ec4\u5408\u800c\u8a00\uff0c\u5b83\u53ef\u4ee5\u5728 ss \u4e2d\u5339\u914d\u4efb\u610f\u81ea\u7136\u6570\u4e2a\u5b57\u7b26\uff0c\u5e76\u4e0d\u5177\u6709\u552f\u4e00\u6027\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u52a8\u6001\u89c4\u5212\uff0c\u5bf9\u5339\u914d\u7684\u65b9\u6848\u8fdb\u884c\u679a\u4e3e\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7528 f[i][j] \u8868\u793a s \u7684\u524d i \u4e2a\u5b57\u7b26\u4e0e p \u4e2d\u7684\u524d j \u4e2a\u5b57\u7b26\u662f\u5426\u80fd\u591f\u5339\u914d\u3002\u5728\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u65f6\uff0c\u6211\u4eec\u8003\u8651p \u7684\u7b2c j \u4e2a\u5b57\u7b26\u7684\u5339\u914d\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>\u5982\u679c p \u7684\u7b2c j \u4e2a\u5b57\u7b26\u662f\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\uff0c\u90a3\u4e48\u6211\u4eec\u5fc5\u987b\u5728 s \u4e2d\u5339\u914d\u4e00\u4e2a\u76f8\u540c\u7684\u5c0f\u5199\u5b57\u6bcd\uff0c\u5373<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"300\" height=\"73\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-177.png\" alt=\"\" class=\"wp-image-1796\"\/><\/figure><\/div>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c s \u7684\u7b2c i \u4e2a\u5b57\u7b26\u4e0e p \u7684\u7b2c j \u4e2a\u5b57\u7b26\u4e0d\u76f8\u540c\uff0c\u90a3\u4e48\u65e0\u6cd5\u8fdb\u884c\u5339\u914d\uff1b\u5426\u5219\u6211\u4eec\u53ef\u4ee5\u5339\u914d\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u5b8c\u6574\u7684\u5339\u914d\u7ed3\u679c\u53d6\u51b3\u4e8e\u4e24\u4e2a\u5b57\u7b26\u4e32\u524d\u9762\u7684\u90e8\u5206\u3002<\/p>\n\n\n\n<p>\u5982\u679c p \u7684\u7b2c j \u4e2a\u5b57\u7b26\u662f *\uff0c\u90a3\u4e48\u5c31\u8868\u793a\u6211\u4eec\u53ef\u4ee5\u5bf9 p \u7684\u7b2c j-1 \u4e2a\u5b57\u7b26\u5339\u914d\u4efb\u610f\u81ea\u7136\u6570\u6b21\u3002\u5728\u5339\u914d 0 \u6b21\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-178.png\" alt=\"\" class=\"wp-image-1798\" width=\"859\" height=\"238\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-178.png 801w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-178-300x83.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-178-768x213.png 768w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/><\/figure>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">\u5982\u679c\u6211\u4eec\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u8fdb\u884c\u8f6c\u79fb\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u679a\u4e3e\u8fd9\u4e2a\u7ec4\u5408\u5230\u5e95\u5339\u914d\u4e86 s \u4e2d\u7684\u51e0\u4e2a\u5b57\u7b26\uff0c\u4f1a\u589e\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u589e\u52a0\uff0c\u5e76\u4e14\u4ee3\u7801\u7f16\u5199\u8d77\u6765\u5341\u5206\u9ebb\u70e6\u3002\u6211\u4eec\u4e0d\u59a8\u6362\u4e2a\u89d2\u5ea6\u8003\u8651\u8fd9\u4e2a\u95ee\u9898\uff1a\u5b57\u6bcd + \u661f\u53f7\u7684\u7ec4\u5408\u5728\u5339\u914d\u7684\u8fc7\u7a0b\u4e2d\uff0c\u672c\u8d28\u4e0a\u53ea\u4f1a\u6709\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"has-light-pink-background-color has-background\"><li><strong>\u5339\u914d\u00a0<em>s<\/em>\u00a0\u672b\u5c3e\u7684\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c06\u8be5\u5b57\u7b26\u6254\u6389\uff0c\u800c\u8be5\u7ec4\u5408\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u8fdb\u884c\u5339\u914d\uff1b<\/strong><\/li><li><strong>\u4e0d\u5339\u914d\u5b57\u7b26\uff0c\u5c06\u8be5\u7ec4\u5408\u6254\u6389\uff0c\u4e0d\u518d\u8fdb\u884c\u5339\u914d<\/strong>\u3002<\/li><\/ul>\n\n\n\n<p>\u5982\u679c\u6309\u7167\u8fd9\u4e2a\u89d2\u5ea6\u8fdb\u884c\u601d\u8003\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u51fa\u5f88\u7cbe\u5de7\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-179.png\" alt=\"\" class=\"wp-image-1800\" width=\"785\" height=\"330\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-179.png 785w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-179-300x126.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-179-768x323.png 768w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/figure><\/div>\n\n\n\n<p>\u7ec6\u8282<\/p>\n\n\n\n<p>\u52a8\u6001\u89c4\u5212\u7684\u8fb9\u754c\u6761\u4ef6\u4e3af[0][0]=true\uff0c\u5373\u4e24\u4e2a\u7a7a\u5b57\u7b26\u4e32\u662f\u53ef\u4ee5\u5339\u914d\u7684\u3002\u6700\u7ec8\u7684\u7b54\u6848\u5373\u4e3a f[m][n]\uff0c\u5176\u4e2d m \u548c n \u5206\u522b\u662f\u5b57\u7b26\u4e32 s \u548c p \u7684\u957f\u5ea6\u3002\u7531\u4e8e\u5927\u90e8\u5206\u8bed\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e0b\u6807\u662f\u4ece 0 \u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u4e0a\u9762\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u72b6\u6001\u4e2d\u6bcf\u4e00\u7ef4\u4e0b\u6807\u4e0e\u5b9e\u9645\u5b57\u7b26\u4e0b\u6807\u7684\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\uff0c\u5982\u679c\u5b57\u7b26\u4e32 p \u4e2d\u5305\u542b\u4e00\u4e2a\u300c\u5b57\u7b26 + \u661f\u53f7\u300d\u7684\u7ec4\u5408\uff08\u4f8b\u5982 a<em>\uff09\uff0c\u90a3\u4e48\u5728\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u65f6\uff0c\u4f1a\u5148\u5c06 a \u8fdb\u884c\u5339\u914d\uff08\u5f53 p[j] \u4e3a a \u65f6\uff09\uff0c\u518d\u5c06 a<\/em> \u4f5c\u4e3a\u6574\u4f53\u8fdb\u884c\u5339\u914d\uff08\u5f53 p[j] \u4e3a * \u65f6\uff09\u3002\u7136\u800c\uff0c\u5728\u9898\u76ee\u63cf\u8ff0\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u5c06 a* \u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u56e0\u6b64\u5c06 a \u8fdb\u884c\u5339\u914d\u662f\u4e0d\u7b26\u5408\u9898\u76ee\u8981\u6c42\u7684\u3002\u770b\u6765\u6211\u4eec\u8fdb\u884c\u4e86\u989d\u5916\u7684\u72b6\u6001\u8f6c\u79fb\uff0c\u8fd9\u6837\u4f1a\u5bf9\u6700\u7ec8\u7684\u7b54\u6848\u4ea7\u751f\u5f71\u54cd\u5417\uff1f\u8fd9\u4e2a\u95ee\u9898\u7559\u7ed9\u8bfb\u8005\u8fdb\u884c\u601d\u8003\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\nclass Solution:\r\n    def isMatch(self, s: str, p: str) -> bool:\r\n        m=len(s)\r\n        n=len(p)\r\n        def matches(i:int,j:int)-> bool:\r\n            if i == 0:\r\n                return False\r\n            if p&#91;j - 1] == '.':\r\n                return True\r\n            return s&#91;i - 1] == p&#91;j - 1]\r\n        f = &#91;&#91;False] * (n + 1) for _ in range(m + 1)]\r\n        f&#91;0]&#91;0]=True\r\n        for i in range(m+1):\r\n            for j in range(1, n + 1):\r\n                if p&#91;j - 1] == '*':\r\n                   f&#91;i]&#91;j] |= f&#91;i]&#91;j - 2]\r\n                   if matches(i, j - 1):\r\n                        f&#91;i]&#91;j] |= f&#91;i - 1]&#91;j]\r\n                else:\r\n                    if matches(i, j):\r\n                        f&#91;i]&#91;j] |= f&#91;i - 1]&#91;j - 1]\r\n        return f&#91;m]&#91;n]<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-180.png\" alt=\"\" class=\"wp-image-1804\" width=\"554\" height=\"140\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-180.png 739w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-180-300x76.png 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;s&nbsp;\u548c\u4e00\u4e2a\u5b57\u7b26\u89c4\u5f8b&nbsp;p\uff0c\u8bf7\u4f60\u6765\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301&nbsp;&#8216;.&#8217;&#038;n &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/11\/leetcode10\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcode10 &#8211;\u6b63\u5219\u5339\u914d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1740"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1740"}],"version-history":[{"count":12,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1740\/revisions"}],"predecessor-version":[{"id":1808,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1740\/revisions\/1808"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}