{"id":742,"date":"2020-09-26T11:17:34","date_gmt":"2020-09-26T03:17:34","guid":{"rendered":"http:\/\/39.96.58.60\/?p=742"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-139-%e5%8d%95%e8%af%8d%e6%8b%86%e5%88%86","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=742","title":{"rendered":"Leetcode 139. \u5355\u8bcd\u62c6\u5206"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a<strong>\u975e\u7a7a<\/strong>\u5b57\u7b26\u4e32 <em>s<\/em> \u548c\u4e00\u4e2a\u5305\u542b<strong>\u975e\u7a7a<\/strong>\u5355\u8bcd\u5217\u8868\u7684\u5b57\u5178 <em>wordDict<\/em>\uff0c\u5224\u5b9a&nbsp;<em>s<\/em> \u662f\u5426\u53ef\u4ee5\u88ab\u7a7a\u683c\u62c6\u5206\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u5728\u5b57\u5178\u4e2d\u51fa\u73b0\u7684\u5355\u8bcd\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u62c6\u5206\u65f6\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u5b57\u5178\u4e2d\u7684\u5355\u8bcd\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u5047\u8bbe\u5b57\u5178\u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5355\u8bcd\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> s = \"leetcode\", wordDict = [\"leet\", \"code\"]\n\n<strong>\u8f93\u51fa:<\/strong> true\n\n<strong>\u89e3\u91ca:<\/strong> \u8fd4\u56de true \u56e0\u4e3a \"leetcode\" \u53ef\u4ee5\u88ab\u62c6\u5206\u6210 \"leet code\"\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> s = \"applepenapple\", wordDict = [\"apple\", \"pen\"]\n\n<strong>\u8f93\u51fa:<\/strong> true\n\n<strong>\u89e3\u91ca:<\/strong> \u8fd4\u56de true \u56e0\u4e3a <code>\"<\/code>applepenapple<code>\"<\/code> \u53ef\u4ee5\u88ab\u62c6\u5206\u6210 <code>\"<\/code>apple pen apple<code>\"<\/code>\u3002\n\n&nbsp;    \u6ce8\u610f\u4f60\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u5b57\u5178\u4e2d\u7684\u5355\u8bcd\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> s = \"catsandog\", wordDict = [\"cats\", \"dog\", \"sand\", \"and\", \"cat\"]\n\n<strong>\u8f93\u51fa:<\/strong> false\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \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\uff1a56 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8645.63% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8686.84% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u603b\u4f53\u7684\u601d\u8def\u662f\u5408\u6210\uff0c\u901a\u8fc7\u4e24\u4e2a\u90fd\u5339\u914d\u7684\u5b50\u4e32\uff0c\u6765\u8bc1\u660e\u5408\u6210\u7684\u4e32 \u53ef\u62c6\u5206\n    \u4f9d\u6b21\u5224\u65ad\u5b50\u4e32\u662f\u5426\u53ef\u4ee5\u62c6\u5206, \u53cc\u6307\u9488\u4f9d\u6b21\u904d\u5386\u3002\u82e5s[:i] and s[i:j] \u90fd\u53ef\u4ee5\u62c6\u5206\uff0c\u5219s[0\uff1aj]\u53ef\u4ee5\u62c6\u5206\n\n    '' l e e t c o d e\n    T  F F F T F F F T\n    i\n       j\n\"\"\"\nclass Solution:\n    def wordBreak(self, s: str, wordDict: List[str]) -&gt; bool:\n        n = len(s)\n        dp = [ False for _ in range(n+1)]\n        dp[0] = True\n        for i in range(n):\n            for j in range(i+1, n+1):\n                if dp[i] and (s[i:j] in wordDict):\n                    dp[j] = True\n        return dp[-1]\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.36% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8665.15% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8fd9\u4e2a\u662f\u53c2\u8003leetcode\u4ed6\u4eba\u63d0\u51fa\u7684\u601d\u8def\n    \u4e0e\u4e0a\u4f8b\u4e0d\u540c\u5728\u4e8e\uff0c\u4e0a\u4f8b\u4e3b\u8981\u601d\u60f3\u662f\u5408\u6210\uff0c\u5148\u627e\u5230\u4e00\u4e2a\u5339\u914d\u7684\u5b50\u4e32\uff0c\u7136\u540e\u5411\u540e\u5339\u914d\uff0c\u518d\u627e\u4e00\u4e2a\u5b50\u4e32\uff0c\u7136\u540e\u5408\u6210\u7684\u5b50\u4e32\u53ef\u62c6\u5206 s[:i]+s[i:j] = s[:j]\n    \u904d\u5386\u4e00\u4e2a\u5b50\u4e32\uff0c\u7136\u540e\u53bb\u67e5\u770b\u8be5\u5b50\u4e32\u662f\u5426\u53ef\u4ee5\u62c6\u5206\uff0c\u82e5\u53ef\u62c6\u5206\uff0c\u5219s[:j] + s[j:i] == s[i] \u5f53\u524d\u5b50\u4e32\u53ef\u62c6\u5206\n\"\"\"\nclass Solution:\n    def wordBreak(self, s: str, wordDict: List[str]) -&gt; bool:\n        n = len(s)\n        dp = [ False for _ in range(n+1)]\n        dp[0] = True\n        for i in range(n+1):\n            for j in range(i-1, -1, -1):\n                if dp[j] and (s[j:i] in wordDict):\n                    dp[i] = True\n                    break\n        return dp[-1]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e32 s \u548c\u4e00\u4e2a\u5305\u542b\u975e\u7a7a\u5355\u8bcd\u5217\u8868\u7684\u5b57\u5178 wordDict\uff0c\u5224\u5b9a&nbsp;s \u662f\u5426\u53ef\u4ee5\u88ab\u7a7a\u683c\u62c6\u5206&#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\/742"}],"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=742"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":743,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/742\/revisions\/743"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}