{"id":2114,"date":"2016-08-21T21:24:27","date_gmt":"2016-08-21T13:24:27","guid":{"rendered":"http:\/\/boweihe.me\/?p=2114"},"modified":"2016-08-21T21:24:27","modified_gmt":"2016-08-21T13:24:27","slug":"leetcode-139-word-break-2","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2114","title":{"rendered":"LeetCode 139. Word Break"},"content":{"rendered":"<p>Given a string <i>s<\/i> and a dictionary of words <i>dict<\/i>, determine if <i>s<\/i> can be segmented into a space-separated sequence of one or more dictionary words.<br \/>\nFor example, given<br \/>\n<i>s<\/i> = <code>\"leetcode\"<\/code>,<br \/>\n<i>dict<\/i> = <code>[\"leet\", \"code\"]<\/code>.<br \/>\nReturn true because <code>\"leetcode\"<\/code> can be segmented as <code>\"leet code\"<\/code>.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u5b57\u7b26\u4e32\u5904\u7406\u7684\u4e00\u4e2a\u9898\u76ee\uff0c\u9996\u5148\u60f3\u5230\u7684\u529e\u6cd5\u5c31\u662f\u7a77\u4e3e\uff1a\u4ece\u5934\u5f00\u59cb\u627eN-letter word\uff0c\u67e5\u4e00\u4e0b\u5728\u4e0d\u5728\u5b57\u5178\u91cc\u5457\u3002\u4f46\u662f\u7eaf\u7a77\u4e3e\u7684\u8bdd\u663e\u7136\u5c31\u4f1a\u8d85\u65f6\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u6210\u4e0b\u9762\u7684\u5b50\u95ee\u9898\uff0c\u5373\u7ed9\u5b9a\u5b57\u7b26\u4e32s\u53ca\u8d77\u59cb\u4f4d\u7f6estart\uff0c\u5bfb\u627es[start&#8230;end]\u80fd\u4e0d\u80fd\u591f\u88ab\u62c6\u5206\u3002\u8fd9\u4e2a\u5b50\u95ee\u9898\u5728\u66b4\u529b\u641c\u7d22\u7684\u65f6\u5019\u5f88\u5bb9\u6613\u88ab\u91cd\u590d\uff0c\u6240\u4ee5\u5f97\u627e\u4e2a\u4e1c\u897f\u8bb0\u5f55\u4e00\u4e0b\u72b6\u6001\uff0c\u78b0\u5230\u76f8\u540c\u7684\u5b50\u95ee\u9898\u5c31\u4e0d\u7528\u518d\u53bb\u67e5\u5b57\u5178\u4e86\u3002<br \/>\n\u6240\u4ee5\uff0c\u4ed6\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u7684\u9898\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">bool wordBreakSub(const string&amp; s, unordered_set&lt;string&gt;&amp; wordDict, int startPos, int* memory)\n{\n    if(memory[startPos] != -1)\n        return memory[startPos]!= 0;\n    string subStr;\n    bool canBreak = false;\n    for(int len=1; len &lt;= s.size() - startPos; len++)\n    {\n        subStr = s.substr(startPos, len);\n        if(wordDict.count(subStr) &gt; 0) {\n            if (startPos + len == s.size()) {\n                memory[startPos] = 1;\n                return true;\n            }\n            canBreak = wordBreakSub(s, wordDict, startPos + len, memory);\n        }\n        if(canBreak)\n            break;\n    }\n    memory[startPos] = canBreak ? 1 : 0;\n    return canBreak;\n}\nbool wordBreak(string s, unordered_set&lt;string&gt;&amp; wordDict)\n{\n    int* memory = new int[s.size()];\n    for(int i=0; i&lt;s.size(); i++)\n        memory[i] = -1; \/\/-1:untest; 0-false; 1-true\n    return wordBreakSub(s, wordDict, 0, memory);\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = &#8220;leetcode&#8221;, dict = [&#8220;leet&#8221;, &#8220;code&#8221;]. Return true because &#8220;leetcode&#8221; can be segmented as &#8220;leet code&#8221;. \u601d\u8def \u5b57\u7b26\u4e32\u5904\u7406\u7684\u4e00\u4e2a\u9898\u76ee\uff0c\u9996\u5148\u60f3\u5230\u7684\u529e\u6cd5\u5c31\u662f\u7a77\u4e3e\uff1a\u4ece\u5934\u5f00\u59cb\u627eN-letter word\uff0c\u67e5\u4e00\u4e0b\u5728\u4e0d\u5728\u5b57\u5178\u91cc\u5457\u3002\u4f46\u662f\u7eaf\u7a77\u4e3e\u7684\u8bdd\u663e\u7136\u5c31\u4f1a\u8d85\u65f6\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u6210\u4e0b\u9762\u7684\u5b50\u95ee\u9898\uff0c\u5373\u7ed9\u5b9a\u5b57\u7b26\u4e32s\u53ca\u8d77\u59cb\u4f4d\u7f6estart\uff0c\u5bfb\u627es[start&#8230;end]\u80fd\u4e0d\u80fd\u591f\u88ab\u62c6\u5206\u3002\u8fd9\u4e2a\u5b50\u95ee\u9898\u5728\u66b4\u529b\u641c\u7d22\u7684\u65f6\u5019\u5f88\u5bb9\u6613\u88ab\u91cd\u590d\uff0c\u6240\u4ee5\u5f97\u627e\u4e2a\u4e1c\u897f\u8bb0\u5f55\u4e00\u4e0b\u72b6\u6001\uff0c\u78b0\u5230\u76f8\u540c\u7684\u5b50\u95ee\u9898\u5c31\u4e0d\u7528\u518d\u53bb\u67e5\u5b57\u5178\u4e86\u3002 \u6240\u4ee5\uff0c\u4ed6\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u7684\u9898\u3002 \u4ee3\u7801 bool wordBreakSub(const string&amp; s, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,141],"class_list":["post-2114","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-141"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2114"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2114\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}